Threads

In this section you should be able to:

1.      Write code to define, instantiate and start new threads using both java.lang.Thread and java.lang.Runnable.

2.      Recognise conditions that might prevent a thread from executing.

3.      Write code using synchronized, wait, notify and notifyAll to protect against concurrent access problems and to communicate between threads.

4.      Define the interaction among threads and object locks when executing synchronized, wait, notify or notifyAll.

 

Threads

A thread is a process in execution within a program. Within a program each thread defines a separate path of execution.

 

Creation of a thread

A thread can be created in two ways.

1.      By implementing the Runnable interface. The Runnable interface consists of only one method – the run method. The run method has a prototype of

public void run();

2.      By extending the Thread class.

 

Execution of a thread

To execute a thread, the thread is first created and then the start() method is invoked on the thread. Eventually the thread would execute and the run method would be invoked. The example below illustrates the two methods of thread creation. You should note that the run method is not invoked directly.

Example – Creation of the Thread by extending the Thread class

public class ThreadExample extends Thread

{

     public void run()

     {

          System.out.println(“Thread Started”);

     }// end of run method

 

     public static void main(String[] args)

     {

          ThreadExample t = new ThreadExample();

          t.start();

     }// end of main method

}// end of ThreadExample class

 

When the run method ends, the thread is supposed to “die”. The next example shows the creation of a thread by implementing the Runnable interface.

Example – Creating a thread by implementing Runnable

public class ThreadExample2 implements Runnable

{

     public void run()

     {

          // Code which gets executed when thread is executed.

     }// end of run method

 

     public static void main(String[] args)

     {

          ThreadExample2 Tt = new ThreadExample2();

          Thread t = new Thread(Tt);

          t.start();

     }// end of main method

}// end of ThreadExample2

 

States of Thread

A thread can be in one of the following states – ready, waiting for some action, running and dead. These are the states explained below.

·        Running state – A thread is said to be in  a running state when it is being executed. This thread has access to the CPU.

·        Ready state – A thread in this state is ready for execution, but is not currently being executed. Once a thread in the ready state gets access to the CPU, it gets converted to running state.

·        Dead state – A thread reaches “dead” state when the run method has finished execution. This thread cannot be executed now.

·        Waiting state – In this state the thread is waiting for some action to happen. Once that action happens, the thread gets into the ready state. A waiting thread can be in one of the following states – sleeping, suspended, blocked and waiting for monitor.

 

Yielding to other processes

A CPU intensive operation being executed may not allow other threads to be executed for a “large” period of time. To prevent this it can allow other threads to execute by invoking the yield() method. The thread on which yield() is invoked would move from running state to ready state.

 

Sleep state of a thread

A thread being executed can invoke the sleep() method to cease executing, and free up the CPU. This thread would go to the “sleep” state for the specified amount of time, after which it would move to the “ready” state. The sleep() method has the following prototypes.

public static void sleep(long millisec)

     throws InterruptedException;

 

public static void sleep(long millisec, int nanosec)

     throws InterruptedException;

 

Synchronized state

A code within the synchronized block is “atomic”. This means only one thread can execute that block of code for a given object at a time. If a thread has started executing this block of code for an object, no other thread can execute this block of code (or any other block of synchronized code) for the same object.

public synchronized void synchExample()

{

/*

     A set of synchronized statements. Assume

     Here that x is a data member of this class

*/

     If(x==0)

          X=1;

}

 

 

 

Back