Thursday, June 27, 2013

Threads: How to Kill a thread?

I was thinking to improve my threading fundamentals, so I planned to make few notes for myself that will assist me in future (Obviously at the time of Interviews). Following questions and answers have been taken from stackoverflow.com. This post is a sticky notes post for me.

Java has provided a stop method to stop the thread gracefully, but with time java has deprecated the method. So what can be most convenient way to exit the thread gracefully?

In the link mentioned below Sun has provided the reason to deprecating few important methods.

Thread.stop deprecated???
On calling Thread.stop, thread unlocks all monitors locked. Basically Thread.stop throws ThreadDeath exception, which propagated up in the stack.)

Now monitor might leave objects in inconsistent state. These objects are called damaged objects.
Objects, with inconsistent state, can be viewed and grabbed by another threads, and it can result in program’s unprecedented behavior.

When unchecked exception is thrown, then user must be aware this exception and he must know about the inconsistency in the behavior. Why did this not happen?

Unlike other unchecked exception, ThreadDeath exception is designed to work silently without leaving much information. So it is not possible to know any inconsistent behavior of the application beforehand.

If not Thread.stop, then what?
In the same document two alternatives have been provided. We need to implement one among them for exit/stop/kill a thread gracefully.

Method 1: We must use a variable to identify when thread should be stopped. It is better to use a volatile variable which will maintain the fresh copy of the specific variable.
   
    private Thread blinker;
    public void stop() {
        blinker.stop();  // UNSAFE!
    }
    public void run() {
        Thread thisThread = Thread.currentThread();
        while (true) {
            try {
                thisThread.sleep(interval);
            } catch (InterruptedException e){
            }
            repaint();
        }
    }

This piece of code must be replaced with the following code.

    private volatile Thread blinker;

    public void stop() {
        blinker = null;// In the stop method, blinker object’s state has been changed.
    }

    public void run() {
        Thread thisThread = Thread.currentThread();
        while (blinker == thisThread) {//When application should be stopped, then blinker is null so it will exit gracefully.
            try {
                thisThread.sleep(interval);
            } catch (InterruptedException e){
            }
            repaint();
        }
    }

Method 2: This method is more convenient when thread waits for longer amount of time. In this case we need to interrupt the waiting thread. In this case stop method should be replaced with the following code.
   public void stop() {
        Thread moribund = blinker;
        blinker = null;//I have my own doubts about this method, I will test it and confirm it soon.
        moribund.interrupt();
    }
For this technique to work, it's critical that any method that catches an interrupt exception and is not prepared to deal with it immediately reasserts the exception. We say reasserts rather than rethrows, because it is not always possible to rethrow the exception. If the method that catches the InterruptedException is not declared to throw this (checked) exception, then it should "reinterrupt itself" with the following incantation:

    Thread.currentThread().interrupt();


This ensures that the Thread will reraise the InterruptedException as soon as it is able.

Why reassertion is required?
This is done to keep state.
When you catch the InterruptException and swallow it, you essentially prevent any higher level methods/thread groups from noticing the interrupt. Which may cause problems.

By calling Thread.currentThread.interrupt(), you set the interrupt flag of the thread, so higher level interrupt handlers will notice it and can handle it appropriately.

Here is one example.

import java.util.Calendar;

class RunnableImpl implements Runnable {
public void run() {
runSomeCode();
}

private void runSomeCode() {
while(!Thread.currentThread().isInterrupted()){
waitForSometime(1000);
System.out.println("Checking for interruption. " + Calendar.getInstance().getTimeInMillis());
}
}

private void waitForSometime(int i) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
}

public class TestRunnable {
public static void main(String[] args) {
Thread instance = new Thread(new RunnableImpl());
instance.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
instance.interrupt();
}
}

Output:
Checking for interruption. 1372386050062
Checking for interruption. 1372386051063
Checking for interruption. 1372386052063
Checking for interruption. 1372386053063
Checking for interruption. 1372386054063
Checking for interruption. 1372386055063
Checking for interruption. 1372386056063
Checking for interruption. 1372386057063
Checking for interruption. 1372386058064
java.lang.InterruptedException: sleep interrupted
Checking for interruption. 1372386058903
at java.lang.Thread.sleep(Native Method)
at RunnableImpl.waitForSometime(TestRunnable.java:20)
at RunnableImpl.runSomeCode(TestRunnable.java:13)
at RunnableImpl.run(TestRunnable.java:8)
at java.lang.Thread.run(Unknown Source)

No comments:

Post a Comment