I have a litte race condition in my current android instrumentation test. What I want is:
With step 1 and 3 being Android live cycle events. But because in the instrumentation test everything happens very fast I get:
Sure I could add a few sleeps to get the desired behaviour but I wonder if there is better way to do it. i.E. is there a way to make sure the thread which was just start ()
-ed did actually start for good and is not still sitting in some scheduling queue awaiting start-up.
(Andy boy, do I miss Ada's rendezvous based multitasking)
And to answer mat's question:
if (this.thread != null && this.thread.isAlive ())
{
this.stop.set (true);
try
{
this.thread.join (1000);
}
catch (final InterruptedException Exception)
{
android.util.Log.w (Actor.TAG, "Thread did not want to join.", Exception);
} // try
} // if
As I said: no-op when because the thread has not started yet.
I typically use a CountDownLatch e.g. see this answer on testing asynchronous processes.
If you want to synchronise the starting of many threads you can also use a CyclicBarrier.
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