I want to find out if an instance of a certain thread is running and so i can use it. Is this possible and how do i do that? Any strategies or suggestions?
For example say i have this thread:
public class SomeThread extends Thread{
ArrayList<String> stringArray=new ArrayList<String>();
public void run(){
//some long time operation or messaging going on
}
public ArrayList<String> getList(){
return stringArray;
}
}
public abstract class AnyActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(//any an instace of SomeThread exist){
ArrayList<String> array=//somehow.getList();
} else {
SomeThread thread=new SomeThread();
thread.setPriority(Thread.MAX_PRIORITY);
thread.start();
}
}
Edit 1: I don't want to pass a reference of the thread because this might be the only place i start it.Think as SomeThread has major importance and starts with the application. I need to ask it to something via Messenger
's or it might tell the activity to do something in the same way.I have updated the example code and set the AnyActivity abstract.All the activities in the application extends this class so i don't know if the thread has been created and started before which means i can't rely on references.
So i cant work with thread.isAlive()
and there is no static method SomeThread.isAlive()
. I cound use setName()
inside the thread but how can i find it afterwards by its name?