Tuesday, August 2, 2016

Thread interruption in java

Thread interruption in java is a mechanism in which a thread which is either sleeping or waiting can be made to stop sleeping or waiting. Thread interruption is like telling the thread that it should stop waiting or sleeping and return to running status. Thread interruption is programmatically implemented using interrupt() method of java.lang.Thread class. interrupt() method is a non-static public method of Thread class. Here is the method signature of interrupt() method.
The whole thread interruption mechanism depends on an internal flag called interrupt status. The initial value of this flag for any thread is false. When you call interrupt() method on a thread, interrupt status of that thread will be set to true. When a thread throws InterruptedException, this status will be set to false again. Remember, InterruptedException is thrown when a thread is interrupted while it is sleeping or waiting. Many methods of Thread class like sleep(), wait(), join() throw InterruptedException.

The 3 methods provided by the Thread class for interrupting a thread
·         public void interrupt()
·         public static boolean interrupted()
·         public boolean isInterrupted()

Here is an example for interrupting a sleeping thread using interrupt() method.

public class ThreadsInJava
{
    public static void main(String[] args)
    {
        Thread t = new Thread()
        {
            public void run()
            {
                try
                {
                    Thread.sleep(10000);        //Thread is sleeping for 10 seconds
                }
                catch (InterruptedException e)
                {
                    System.out.println("Thread is interrupted");
                }
            }
        };

        t.start();

        try
        {
            Thread.sleep(3000);     //Main thread is sleeping for 3 seconds
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        t.interrupt();         //main thread is interrupting thread t
    }
}
In the above example, main thread is creating and starting thread t. Thread t is going to sleep for 10 seconds as soon as it started. main thread, after starting thread t, is also going to sleep for 3 seconds. After sleeping for 3 seconds, main thread calls interrupt() method on thread t. It interrupts sleeping thread t. It causes the InterruptedException.

Some Things-To-Remember About Thread Interruption In Java:
·         You can check whether a particular thread is interrupted or not usingisInterrupted() method of Thread class. This method returns current interrupt status of a thread.
·         interrupt() method will throw SecurityException if current thread can not interrupt a calling thread.
·         A thread can interrupt itself. i.e a thread can call interrupt() method on it’s own.
·         There is one more method to check interrupt status of a thread, called interrupted() method. It is a static method of Thread class. It also returns the current interrupt status of a thread like isInterrupted() method. But, it clears interrupt status of a thread. i.e if interrupt status of a thread is true, then it will set the status to false.

public class ThreadsInJava
{
    public static void main(String[] args)
    {
        Thread t = new Thread()
        {
            public void run()
            {
                System.out.println(interrupted());         //Output : false
                 interrupt();
                 System.out.println(interrupted());        //Output : true
                 System.out.println(interrupted());       //Output : false
            }
        };
         t.start();
    }
}

No comments:

Post a Comment