Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 236
wajiw Avatar asked Dec 03 '10 18:12

wajiw


People also ask

How do I know if a thread is running?

You can use: Thread. currentThread(). isAlive(); . Returns true if this thread is alive; false otherwise.

What is the method used to find the thread is running state or not?

Explanation: isAlive() method is used to check whether the thread being called is running or not, here thread is the main() method which is running till the program is terminated hence it returns true.

How can you tell if a thread is main thread?

if(Looper. getMainLooper(). getThread() == Thread. currentThread()) { // Current Thread is Main Thread. }


1 Answers

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().

like image 116
EboMike Avatar answered Sep 21 '22 15:09

EboMike