Bonk
Bonk

Reputation: 1949

Thread interruptions are not caught by InterruptedException

I have a controller and a thread that does some work, the controller has an interrupt function that shuts off the threads in emergency situation.

the skeleton of my code looks something like this:

public class SomeController{

private Thread th;
public SomeController(){
    th = null;
}
public void Input(int state){
    switch(state){
    case 0: //emergency shut off
        if(th != null){
            th.sleep(1000); //make thread sleep first, still no effect
            th.interrupt();
         }
        break;
    case 1: //thread creation
        th = new Thread(new Runnable(){
            public void run(){
                try{
                    DoSomeWork();
                    }
                catch(InterruptedException e)
                {
                    EmergencyProcedures();
                }
            }
            });
        th.start();
        break;
    }
}

However, when interrupt is called, the InterruptedException is never caught. What am I doing wrong here?

Upvotes: 1

Views: 1038

Answers (6)

Marko Topolnik
Marko Topolnik

Reputation: 200256

At least this is a problem with your code:

th.sleep(1000); //make thread sleep first, still no effect

You can't make another thread sleep: you'll make the current thread sleep only. You are just calling the static sleep method through an instance of Thread.

Calling th.interrupt() will not automatically cause the InterruptedException to be thrown within the other thread. That will happen only if the thread's code enters an interruptible method (one that declares InterruptedException).

Your main misconception (probably) is with the nature of the interrupt mechanism. Unlike the deprecated stop mechanism, interrupt is cooperative and therefore optional: the thread only receives an interrupt signal and is responsible to check its state explicitly. It can respond to the signal in any way it sees fit.

Upvotes: 4

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41230

See first thing you have created Thread th but you did not start it. Second thing you can not catch InterruptedException exception inside run method. Please try this in switch status 1.

           th = new Thread(new Runnable(){
            public void run(){
                try{
                    System.out.println("Run");
                    Thread.sleep(10000);
                    }
                catch(InterruptedException e)
                {
                    EmergencyProcedures();
                }
            }
            });

Upvotes: 0

lynks
lynks

Reputation: 5689

Assuming you are correctly calling th.interrupt();, it must be the case that the interrupt is not being 'noticed' in any reasonable amount of time. Interrupts do not magically get thrown the moment you call interrupt() on a thread. The thread, generally speaking, must detect when it has been interrupted, and throw its own InterruptedException.

Now, this is not usually a problem, as Thread.sleep() and a few other commonly used methods are designed to throw an InterruptedException immediately.

You need to check Thread.interrupted() once in a while manually, this will tell you whether or not the thread has been interrupted, which gives you a construct something like;

if (Thread.interrupted()) {
    throw new InterruptedException();
}

Which you would put inside DoSomeWork();.

DoSomeWork(); does throw InterruptedException right?

Upvotes: 2

RNJ
RNJ

Reputation: 15562

Are you interrupting after the sleep has finished. As per the docs

If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.

Upvotes: 0

assylias
assylias

Reputation: 328845

The only possibilities that come to mind:

  • you are not interrupting the thread (are you sure th.interrupt() is called?)
  • you interrupt another thread
  • the thread gets interrupted but there is a problem in EmergencyProcedures that makes you think it was not interrupted
  • you never start the thread and therefore you can't interrupt it.
  • DoSomeWork() ignores the interruption

Upvotes: 5

dan
dan

Reputation: 13272

Like @assylias said, or your thread is not supporting an interruption, see Supporting Interruption section from: http://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html. Basically you need to use a method that supports the interruption, like sleep or Thread.interrupted().

Upvotes: 1

Related Questions