0

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?

bugraoral
  • 2,630
  • 1
  • 21
  • 25

5 Answers5

1

Found the solution, thanks for the guidance

private SomeThread getSomeThread(){
    Thread thread=null;
    int i=0;

    Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
    Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]);
    while(thread==null && i<threadArray.length){
        if(threadArray[i].getName().equals(SomeThread.class.getSimpleName()))
            thread=threadArray[i];
        i++;
    }

    return (SomeThread)thread;  
}
bugraoral
  • 2,630
  • 1
  • 21
  • 25
0

You need to pass the reference of the Thread you are interested in later/further to you code and then test it with :

  Thread t; t.isAlive();

P.S. You could tell use exactly what you want to do and probably we can help you more.

Eugene
  • 117,005
  • 15
  • 201
  • 306
0

thread.isAlive() is your friend: http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html#isAlive()

TomTasche
  • 5,448
  • 7
  • 41
  • 67
0

try this

Get a List of all Threads currently running in Java

you can also get a thread by its name or id

http://nadeausoftware.com/articles/2008/04/java_tip_how_list_and_find_threads_and_thread_groups#Gettingathreadbyname

Community
  • 1
  • 1
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0
 if( SomeThread.isAlive())
 {
       // in running state 
 }

isAlive() method returns true if it is running else false

Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133