valdrus (valdrus) wrote in java_faq,
valdrus
valdrus
java_faq

How can I tell what state a thread is in ?

Prior to Java 5, isAlive() was commonly used to test a threads state. 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.
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your IP address will be recorded 

  • 0 comments