Thread states:
So far we have  talked  about starting and  running threads and  having  access to the CPU in a very general way. What we need to do now is look at the various states that a thread can be in and how a thread might move from one state to another.
A thread is in one of five states at any moment in time. These are:
- initial state; 
- runnable state;
- running  state;
- blocked state;
- finished state. 
Figure shows the possible transitions between states.

Figure: Thread  states showing  possible transitions
Movement  from one  state to another is governed by a number of methods which are inherited  from the Thread class and  the Object class, as  well as  by the operating system and  other  external  factors. We have  already met some of these methods, but there  are  others that we will use  in order  to make  full use  of threads.
These methods allow some control over which state a thread is in, although you must be aware that the operating system can  also  change the state of a thread and  so your program must  anticipate this possibility.
Figure shows  a thread which is put into the initial  state by means of it being created using  new.

Figure: Transition  to the initial state
In Figure, we see that the start method moves the thread into the runnable state. This does not mean that it will run immediately (there may be another thread that is being run by the CPU).

Figure: Transition  from the initial state  to the runnable state
At certain times  during  the execution of a program that contains threads, the Java system will have  a look at all the runnable threads and  will select one to execute (which one it selects is discussed later). Only the thread that is actually  executing is said  to be in the running state. This is shown  in Figure.

Figure Transitions between the runnable and  running  states
When a particular thread is chosen, its run method is invoked and  the thread can  begin to execute the code. It is now in the running  state. When it finishes executing, it moves into the finished state. However,  the code may not reach its conclusion before other  events intervene. The thread may run out of its allotted  CPU time, as  represented by the time  out  label  in Figure. Alternatively, it may invoke the yield method to give way to other threads of equal or higher priority. Both scenarios will cause the thread to go back into the runnable state where  it can,  at some point,  be chosen by the Java run-time system to move back into the running  state. Once it is back in the running  state it can  carry  on with the execution of its run method at the point where  it left off. This is indicated by the resume label  in Figure.
We already mentioned that a thread in the running  state may run to completion and hence enter  the finished state. In Figure, we see that it is also possible that a running thread may enter  the blocked state.

Figure: Transitions from the running  state  to the blocked  and  finished  states
From the running  state, a thread may move  to the blocked state because of delays in I/O, or because the sleep method or wait method is invoked.  The sleep method is a static method of Thread and  so can  be called in any program, even those that do not use  the Thread class. When invoked,  it puts  the thread to sleep for the specified number of milliseconds.
In Figure, we see the transition  from the blocked state to the runnable state.

Figure: Transition  from the blocked  state  to the runnable state
In the blocked state, the thread cannot progress until something happens - for example, an I/O transfer is completed. Note that from the blocked state the thread can  move only into the runnable state to await being chosen by the Java run-time system to move  into the running  state.
Three  of the methods that we have  seen so far have  not been discussed in any detail and  they are  wait, notify and  notifyAll. All three  methods are  inherited  from the Object class rather  than  the Thread class, and  we will see why this is so later. Briefiy, the wait method causes a thread to wait for notification by another thread, and  notify and  notifyAll provide such notification.  The situation  is more  complicated than  this. 
There  are  other  ways  of changing the state of a thread, and  later in this unit we will describe them. The important  concept at this stage is that a Java program may involve a number of threads, some of which are  runnable and  ready to be  executed by a processor and  others that are  unable to proceed because they are  blocked. On a single-processor machine only one  thread can  actually  be  running  at a time. The Java system has  a component known as  the scheduler that makes decisions about which thread to run next. The decision as to when thread execution is switched will depend on the underlying operating system: some operating systems switch  when  the currently running  thread becomes blocked, while others allocate set  blocks of time to a thread.
 
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Thread states homework and assignments? Live Thread states experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Thread states homework help, java assignment help and Thread states projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours