Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding Interrupt & isInterrupted Method in Thread Class

In our application we have threads doing a specific action. On receive of an event, we need to interrupt those threads in progress. These threads would check their interrupt status by calling "Thread.currentThread().isInterrupted()", if it is interrupted, then the thread stops by returining from run method.

Problem we are seeing now is these threads can wait() on a object. If the thread is interrupted while wait() on an object, the Threads interrupt status is reset and InterruptedException is thrown.

Since 1) we have many places where the thread can wait on an object 2) the methods of other objects which the thread access can wait on an object

we have decided to override the interrupt() & isInterrupted() method. The code would look something like

public class MyThread extends Thread {

    private boolean isInterrupted = false;

    @Override 
    public void interrupt() {
        isInterrupted = true;
        super.interrupt();
    }

    @Override
    public boolean isInterrupted() {
        return isInterrupted;
    }

    public void run() {
        .........
        .........

        try {
            object1.wait();
        } catch (InterruptedException e) {
            /* if interrput() is not overrided, the interrupted flag 
            would be reset */
        }

        if(Thread.currentThread().isInterrupted()) {
            /* if interrupt is not overrided, isInterrupted would return
            false and the thread would continue to run. */
            return;
        }

        object2.method1(); // method1() can also wait on another object
        /* In method1() also we would need to check isInterrupted and get out
        quickly as soon as possible. This method1() will also be used by
        other threads which will not be interrupted. */

        if(Thread.currentThread().isInterrupted()) {
            return;
        }
        .........
        .........
    }
}

So my questions are

1) Is overriding interrupt() and isInterrupted() as above is a good practice to code?

2) While overriding interrupt() should we maintain the same behavior i.e. throw interrupted and other exceptions as stated in the java doc of interrupt() method in Thread class? Actually we need the threads that are waiting to be interrupted but the interrupted flag to be set.

3) What about Thread.interrupted() method which is static. With the above overriding we shouldn't use this method right?

like image 283
Arun Avatar asked Nov 25 '25 16:11

Arun


1 Answers

First of all, overriding these methods is a very bad idea.

Second, I don’t get why you are making your life that hard:

try {
  doSomethingThatCanBlock();// e.g. wait()
}
catch(InterruptedException ex) {
  Thread.currentThread().interrupt(); // restore interrupt state
}

Now, the next time you check the interrupted state of your Thread it will tell you the appropriate state. That’s it.

like image 95
Holger Avatar answered Nov 28 '25 06:11

Holger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!