PDA

Click to See Complete Forum and Search --> : How can I print all thread states?


Dillinger4
Jul 13th, 2005, 12:06 AM
The following code works fine but i am having trouble working with the Enum that is returned from getState(). In the following i guess the Enum returned will always contain just one element since a thread can only be in one state at a time.

public static void ThreadState(Thread t){
Enum e = t.getState();
System.out.println(t.getName() + " is " + e.name());
}

but how can i print out all the states? Enum defines

static Thread.State[] values()

I have the following......

Enum e = t.getState();
Thread.State[] ts = e.values();

t is simply a Thread instance but the compiler says that e has no values() method.

Dillinger4
Jul 13th, 2005, 12:55 PM
Got it but a bit confused. If getState() is supposed to return an Enum of thread states then how is it possible to assign the Enum to a single Thread.State? Is it because a thread can only be in a single state at a time?

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]);
}