javaandroidmultithreading

How to check if an Android Thread is running


Is there a way to check if a Thread object has had start called on it already?

I'm trying to so something like:

if(rt.isAlive() == true)
{
    Log.v(TAG, "START RECORD");
    rt.recording = true;
}
else
{
    Log.v(TAG, "START THREAD/RECORD");

    rt.start();
}

where it would start the thread if it's not already running.


Solution

  • Assuming that rt is a Thread, just check rt.isAlive().

    Alternatively, just use a boolean flag and set it to true right before you start your thread.

    I would actually prefer the boolean approach so there is no way that the main thread could start the other thread twice - there may be a short delay until your Thread is up and running, and if your main thread tries to start the thread twice in quick succession, it may get a "false" negative on rt.isAlive().