Friday 15 May 2015

Thread States in Java

 Thread behavior is completely dependent on the state a thread is in. The state of a thread defines its current mode of operation, such as whether it is running or not. Following is a list of the Java thread states:
  • New
  • Runnable
  • Not running
  • Dead

New

A thread is in the "new" state when it is first created until its start method is called. New threads are already initialized and ready to get to work, but they haven't been given the cue to take off and get busy.

Runnable

When the start method is called on a new thread, the runmethod is in turn called and the thread enters the "runnable" state. You may be thinking this state should just be called "running," because the execution of the runmethod means a thread is running. However, you have to take into consideration the whole priority issue of threads having to potentially share a single CPU. Even though every thread may be running from an end-user perspective, in actuality all but the one currently accessing the CPU are in a "runnable" wait state at any particular instant. You can still conceptually think of the "runnable" state as the "running" state; just remember that all threads have to share system resources.

Not Running

The "not running" state applies to all threads that are temporarily halted for some reason. When a thread is in this state, it is still available for use and is capable of re-entering the "runnable" state at some point. Threads can enter the "not running" state through a variety of means. Following is a list of the different possible events that can cause a thread to be temporarily halted:
  • The suspendmethod is called.
  • The sleepmethod is called.
  • The waitmethod is called.
  • The thread is blocking for I/O.
For each of these actions causing a thread to enter the "not running" state, there is an equivalent response to get the thread running again. Following is a list of the corresponding events that can put a thread back in the "runnable" state:
  • If a thread is suspended, the resumemethod is called.
  • If a thread is sleeping, the number of specified milliseconds elapse.
  • If a thread is waiting, the object owning the condition variable calls notifyor notifyAll.
  • If a thread is blocking for I/O, the I/O operation finishes.

Dead

A thread enters the "dead" state when it is no longer needed. Dead threads cannot be revived and executed again. A thread can enter the "dead" state through one of two approaches, which follow:
  • The runmethod finishes executing.
  • The stopmethod is called.
The first approach is the natural way for a thread to die; you can think of a thread dying when its runmethod finishes executing as death by natural causes. In contrast to this is a thread dying by way of the stopmethod; calling the stopmethod kills a thread in an asynchronous fashion.

Even though the latter approach sounds kind of abrupt, it is often very useful. For example, it's common for applets to kill their threads using stop when their own stop method is called. The reason for this is that an applet's stopmethod is usually called in response to a user leaving the Web page containing the applet. You don't want threads out there executing for an applet that isn't even active, so killing the threads makes perfect sense. 

No comments:

Post a Comment