Showing posts with label Thread states. Show all posts
Showing posts with label Thread states. Show all posts

Monday, 3 March 2014

Get the state of a thread : getState()

public Thread.State getState()
Returns the state of this thread.

Example
class ThreadStateEx extends Thread{
 public void run(){
  try{
   Thread.currentThread().sleep(500);
  }
  catch(InterruptedException e){
  }
 }

 public static void main(String args[])throws Exception{
  ThreadStateEx t1 = new ThreadStateEx();
  ThreadStateEx t2 = new ThreadStateEx();
  ThreadStateEx t3 = new ThreadStateEx();

  t1.start();
  t2.start();
  t3.start();

  System.out.println("Thread1 state is " + t1.getState());
  System.out.println("Thread2 state is " + t2.getState());
  System.out.println("Thread3 state is " + t3.getState());

  t1.join();
  t2.join();
  t3.join();

  System.out.println("Thread1 state is " + t1.getState());
  System.out.println("Thread2 state is " + t2.getState());
  System.out.println("Thread3 state is " + t3.getState());
 }
}

Output   
Thread1 state is TIMED_WAITING
Thread2 state is TIMED_WAITING
Thread3 state is TIMED_WAITING
Thread1 state is TERMINATED
Thread2 state is TERMINATED
Thread3 state is TERMINATED


Thread State                                                 Wait & notify in Object class                                                 Home

Thread.State

Thread.State is of type enum and represents the states of a thread.

Below Example shows the different states of a thread in Java
   
class ThreadState{
 public static void main(String args[]){
  for(Thread.State c : Thread.State.values())
   System.out.println(c);
   }
}

Output
NEW
RUNNABLE
BLOCKED
WAITING
TIMED_WAITING
TERMINATED

State
Description
NEW
It is the state where thread is created and not yet started
RUNNABLE
A thread in the runnable state is executing in the Java virtual machine but it may be waiting for other resources from the operating system such as processor.
BLOCKED
It is the state, where a thread is blocked to acquire a monitor lock
WAITING
A thread is in waiting state by calling wait() or join() methods with no time outs, I.e, with no parameters passing to them
TIMED_WAITING
A thread is in the timed waiting state due to calling one of the methods sleep, wait, join with a specified positive waiting time
TERMINATED
Thread completes execution or stopped.

Thread.state has two methods

1. public static Thread.State[] values()
Returns the array contains the all enum constants of this type.

2. public static Thread.State valueOf(String name)
Method returns the enum constant with the specified String.

Example
class ThreadState{
 public static void main(String args[]){
  Thread.State const1 = Thread.State.valueOf("NEW");
  Thread.State const2 = Thread.State.valueOf("RUNNABLE");
  Thread.State const3 = Thread.State.valueOf("TERMINATED");

  System.out.println("const1 is " + const1);
  System.out.println("const2 is " + const2);
  System.out.println("const3 is " + const3);
 }
}

Output
const1 is NEW
const2 is RUNNABLE
const3 is TERMINATED


   
Thread states                                                 Get Thread state                                                 Home

Sunday, 2 March 2014

Thread States

1. Once a thread starts executing it will be in one of below states
    a. Running
    b. Ready to Run (Waiting in the ready queue for their turn)
    c. sleeping
    d. waiting
    e blocked

2. Not all threads are interruptible, we can interrupt a thread which is sleeping or which is in waiting state. But if a thread block on I/O it will not respond to interrupts. When a thread is blocked waiting to get the lock required by a synchronized statement, it also will not respond to interrupt requests.

3. We can interrupt a thread that is sleeping 
class ThreadInterrupt implements Runnable{
 public void run(){
  System.out.println("Time to sleep");
  try{
   Thread.currentThread().sleep(1000);
  }
  catch(InterruptedException e){
   System.out.println(e);
  }
 }

 public static void main(String args[]){
  ThreadInterrupt task1 = new ThreadInterrupt();
  Thread t1 = new Thread(task1);
  t1.start();
  t1.interrupt();
 }
}
   
Output
Time to sleep
java.lang.InterruptedException: sleep interrupted

4. We can interrupt a thread that is waiting on particular object
class ThreadInterrupt implements Runnable{
 Object obj = new Object();

 public void run(){
  System.out.println("Time to sleep");
   synchronized(obj){
    try{
     obj.wait();
    }
    catch(InterruptedException e){
     System.out.println(e);
    }
   }
  }

  public static void main(String args[]){
   ThreadInterrupt task1 = new ThreadInterrupt();
   Thread t1 = new Thread(task1);
   t1.start();
   t1.interrupt();
  }
}
   
Output
Time to sleep
java.lang.InterruptedException

5. A Thread is not interruptible if it is waiting on I/O
import java.io.*;
class ThreadInterrupt implements Runnable{
 static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

 public void run(){
  try{
   System.out.println("Enter input");
   String s = br.readLine();
   System.out.println("You entered " +s);
  }
  catch(Exception e){
   System.out.println(e);
  }
 }

 public static void main(String args[]){
  ThreadInterrupt task1 = new ThreadInterrupt();
  Thread t1 = new Thread(task1);
  t1.start();
  t1.interrupt();
 }
}

Output
Enter input
PTR
You entered PTR
   
6. The difference between blocked and waiting state is, a thread which is in waiting state can be interruptible, where as, a thread which is blocked on acquiring lock or I/O is not interruptible


Notify Thread                                                 Thread states                                                 Home