How can i tell what state a thread is in?
Prior to Java 5, isAlive() was commonly used to test is a thread was still executing. If isAlive() returned false the thread was either new or terminated but there was simply no way to differentiate between the two.
Starting with the release of Tiger (Java 5) you can now get what state a thread is in by using the getState() method which returns an Enum of Thread.States. A thread can only be in one of the following states at a given point in time.
- NEW
A Fresh thread that has not yet started to execute. - RUNNABLE
A thread that is executing in the Java virtual machine. - BLOCKED
A thread that is blocked waiting for a monitor lock. - WAITING
A thread that is wating to be notified by another thread. - TIMED_WAITING
A thread that is wating to be notified by another thread for a specific amount of time. - TERMINATED
A thread whos run method has ended.
The following code prints out all thread states.
Code:
public class ThreadStates{
public static void main(String[] args){
Thread t = new Thread();
Thread.State e = t.getState();
Thread.State[] ts = e.values();
for(int i = 0; i < ts.length; i++){
System.out.println(ts[i]);
}
}
}
Below is a simple program, which prints some of the different states that two threads are in at certain points of a program. The code simply creates two threads. The first thread creates a file and fills it with the letters of the alphabet. The second thread opens the same file and reads the contents printing to the console. You will find along the way we have a placed a method a strategic points to determine exactly what state the thread is in at that point in time.
Code:
import java.io.*;
public class ThTest{
public static void main(String[] args){
try{
AlphaBlower ab = new AlphaBlower("AlphaBlower");
ThreadState(ab);
ab.start();
ab.join();
ThreadState(ab);
AlphaSucker as = new AlphaSucker("AlphaSucker");
ThreadState(as);
as.start();
as.join();
ThreadState(as);
}catch(InterruptedException ie){
System.out.println(ie);
}
}
public static void ThreadState(Thread t){
Enum e = t.getState();
System.out.println(t.getName() + " is " + e.name());
}
}
class AlphaBlower extends Thread{
private BufferedWriter bw;
public AlphaBlower(String tname){
super(tname);
}
public void run(){
ThTest.ThreadState(Thread.currentThread());
try{
bw = new BufferedWriter(new FileWriter("C:" + File.separator + "AlphaHolder.txt"));
}catch(IOException ioe){
System.err.println(ioe);
}
try{
for(int alpha = 97; alpha <= 122; ++alpha){
try{
bw.write(alpha);
}catch(IOException ioe){
System.err.println(ioe);
}
}
}finally{
try{
bw.close();
}catch(IOException ioe){
System.out.println(ioe);
}
}
}
}
class AlphaSucker extends Thread{
private int c;
private BufferedReader br;
public AlphaSucker(String tname){
super(tname);
}
public void run(){
ThTest.ThreadState(Thread.currentThread());
try{
br = new BufferedReader(new FileReader("C:" + File.separator + "AlphaHolder.txt"));
}catch(IOException ioe){
System.err.println(ioe);
}
try{
while((c = br.read()) != -1){
System.out.print((char)c);
}
}catch(IOException ioe){
System.err.println(ioe);
}finally{
try{
br.close();
}catch(IOException ioe){System.err.println(ioe);}
}
}
}