java - Using Thread interrupt to wake up a thread? -
are there drawbacks regularly wake thread on android using thread.interrupt. thread loop looks similar this:
public void run() { while(true) { seek { wait(); } catch(interruptedexception e) { performwork(); } } }
yes. it's horrible way code. interrupt()
instance throw exception if thread blocked in i/o , not made used this.
instead, utilize notify/wait made this. in run()
:
synchronized (this) { while (conditionforwaiting) { seek { wait(); } grab (interruptedexception ex) {} } performwork();
and notify thread conditionforwaiting changed:
synchronized (threadinstance) { threadinstance.notify(); }
java android multithreading
No comments:
Post a Comment