Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread with custom states

Is it possible in Java (Android) to implement a customized version of a Thread which carries its own States?

What I mean is: While ThreadA is in Running state, it still can be polled by ThreadB that asks for its state

e.g.
ThreadA.getState();

It is possible to modify the states values to some custom ones? So as to implement a sort of basic communication system between those two threads?

Thanks.

like image 695
2dvisio Avatar asked Jan 16 '23 07:01

2dvisio


1 Answers

Yes that is possible. I used this a lot in my previous projects, all what you need is to extend the Thread class.

public class StateThread extends Thread{
   String state = "ThreadState";
   public synchronized void setState(String newState){
      state = newState;
   }
   public synchronized String getState(){
      return state;
   }
   @override
   public void run(){
      // Do stuff and update state...
   }
}
like image 187
GETah Avatar answered Jan 25 '23 13:01

GETah