Question

How exactly do i call the .interrupt() method? When I have Thread.sleep(1000), when and where do I call the .interrupt() method? is it after? What I want to do is stop Thread.sleep(1000) midway.

EDIT::

i'm having trouble stopping a thread in the middle. This is part of my code, in the StoplightThread class I have problems on the first if statement. What it is supposed to do is wait at least 10 secs then allow the user to press the button so they can change the light, if the button is pressed it should stop the running thread in this case Thread.sleep(40000). What happens is when I press the button it changes the light but does not stop the thread. If I press the button while there is still 20secs left it will add 20secs to the 10secs for the yellow light, making it yellow for 30 secs.

Edit: if you are wondering, stoplightCanvas.x == 3 is green, stoplightCanvas.x == 2 is yellow, and stoplightCanvas.x == 1 is red.

class StoplightCanvas extends Canvas implements ActionListener
{  

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == cross) {
            isPressed = true;
            if (x == 3 && canCross)
                x = 2;     
        }
        repaint();
    }

}


class StoplightThread extends Thread
{
    StoplightCanvas stoplightCanvas;

    StoplightThread(StoplightCanvas stoplightCanvas) {
        this.stoplightCanvas = stoplightCanvas;
    }

    public void run() 
    {
        if (stoplightCanvas.x == 3){
               Thread.sleep(10000);
               stoplightCanvas.canCross = true;
               Thread.sleep(40000);
               if(stoplightCanvas.isPressed)
                   StoplightThread.interrupt();
           } else if (stoplightCanvas.x == 2) {
               Thread.sleep(10000);    
           } else if (stoplightCanvas.x == 1) {
               Thread.sleep(60000);
           }
       } catch (InterruptedException e){}

           stoplightCanvas.toggleColor();
           stoplightCanvas.repaint();
        }           
    }
}
Was it helpful?

Solution

If you were going to call interrupt() at all, you'd call it from a different thread than the sleep().

If you want to interrupt the sleep() midway from the same thread, you can do this:

   Thread.sleep( 500 );
   ... 
   Thread.sleep( 500 );

All that said, sleep() can be a code smell.

EDIT (after OP edit):

Call interrupt() on the StoplightThread from the GUI thread in your actionPerformed() method.

OTHER TIPS

try this example

public class Test1 {

    public static void main(String[] args) throws Exception {
        Thread t = new Thread() {
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        t.start();
        Thread.sleep(1000);
        t.interrupt();
    }
}

it prints

java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at test.Test1$1.run(Test1.java:9)

You need a reference to the thread you want to interrupt, so that you can call interrupt() on it from a different thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top