Tuesday 3 June 2014

Explain Thread life cycle and difference between wait() and sleep() method.

    First to understand the Thread and Process, then we can discuss the thread life cycle in java.

Process:--
         An executing instance of a program is called a process.  Processes have their own address space(memory space). A process can contain multiple threads.
Example,
      Executing multiple instances of the ‘Calculator’ program. Each of the instances are termed as a process.

Thread:--
  
        Threading is a facility to allow multiple activities to coexist within a single process. Most modern operating systems support threads, and the concept of threads has been around in various forms for many years. Java is the first mainstream programming language to explicitly include threading within the language itself, rather than treating threading as a facility of the underlying operating system.

       Threads are sometimes referred to as lightweight processes. Like processes, threads are independent, concurrent paths of execution through a program, and each thread has its own stack, its own program counter, and its own local variables. However, threads within a process are less insulated from each other than separate processes are. They share memory, file handles, and other per-process state.


Thread Life Cycle:-

A thread can be in one of the following states ,
  1. New born state(New)
  2. Ready to run state (Runnable)
  3. Running state(Running)
  4. Blocked state
  5. Dead state

Thread Life Cycle
Thread Life Cycle In Java


New Born State:--
  • The thread enters the new born state as soon as it is created. The thread is created using the new operator.
  • From the new born state the thread can go to ready to run mode or dead state.
  • If start( ) method is called then the thread goes to ready to run mode. If the stop( ) method is called then the thread goes to dead state.


Ready to run mode (Runnable Mode):--
  • If the thread is ready for execution but waiting for the CPU the thread is said to be in ready to run mode. 
  • All the events that are waiting for the processor are queued up in the ready to run mode and are served in FIFO manner or priority scheduling.
  • From this state the thread can go to running state if the processor is available using the scheduled( ) method. 
  • From the running mode the thread can again join the queue of runnable threads. 
  • The process of allotting time for the threads is called time slicing.


Running State:--
  • If the thread is in execution then it is said to be in running state. 
  • The thread can finish its work and end normally. 
  • The thread can also be forced to give up the control when one of the following conditions arise
  1. A thread can be suspended by suspend( ) method. A suspended thread can be revived by using the resume() method.
     2. A thread can be made to sleep for a particular time by using the sleep(milliseconds) method.
         The sleeping method re-enters runnable state when the time elapses.
     3. A thread can be made to wait until a particular event occur using the wait() method, which can
         be run again using the notify( ) method.

Blocked State:--
  • A thread is said to be in blocked state if it prevented from entering into the runnable state and so the running state.
  • The thread enters the blocked state when it is suspended, made to sleep or wait. 
  • A blocked thread can enter into runnable state at any time and can resume execution.
Dead State:--
  • The running thread ends its life when it has completed executing the run() method which is called natural dead. 
  • The thread can also be killed at any stage by using the stop( ) method.


Difference between wait() and sleep() methods in thread:

1) wait is called from synchronized context only while sleep can be called without synchronized block.
2) wait is called on object while sleep is called on thread.
3) waiting thread can be awake by calling notify and notifyAll while sleeping thread can not be
     awaken by calling notify method.
4) wait is normally done on condition, Thread wait until a condition is true while sleep is just to put
    your thread on sleep.
5) wait release lock on object while waiting while sleep doesn't release lock while waiting.


Related Post:-
Thread(or Multithread) interview questions & answers 


No comments:

Post a Comment