Skip to content
Advertisement

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?

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement