Folks,
Here is a simplified code for my background thread:
public class MyThread extends Thread {
private Handler _handler;
public void run() {
Looper.prepare();
this._handler = new Handler();
Looper.loop();
}
public void DoSomething() {
if (!this.isAlive()) {
this.start();
}
this._handler.post(blah);
}
}
The problem I have is that the background thread may not have yet created the handler object when post() call is made. Essentially, I need a wait loop for the handler object to be initialized. What is generated accepted method of doing this under Android?
Thank you in advance for your help.
Regards, Peter
You can set a flag after you initialize the Handler and wait for this flag before calling post
.
An easy way to wait for a flag in a concurrent system is with a CountDownLatch
. It would start at 1 and decrement after the Handler is initialized. Check out the details here:
http://download.oracle.com/javase/1,5,0/docs/api/java/util/concurrent/CountDownLatch.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With