Help interrupting a thread.

playButton.addActionListener(new ActionListener( ){

publicvoid actionPerformed(ActionEvent ev){

worker =new SwingWorker<Matrix, Void>(){

public Matrix doInBackground(){

while (true){

//various code

_robot.delay(6000)

}

}

};

worker.execute();

}

});

stopButton.addActionListener(new ActionListener( ){

publicvoid actionPerformed(ActionEvent ev){

worker.cancel(true);

//System.exit(0);

}

});

Runtime Errors when the stop button is pressed:

run:

java.lang.InterruptedException: sleep interrupted

at java.lang.Thread.sleep(Native Method)

at java.awt.Robot.delay(Robot.java:403)

at App$1$1.doInBackground(App.java:93)

at App$1$1.doInBackground(App.java:54)

at javax.swing.SwingWorker$1.call(SwingWorker.java:279)

at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)

at java.util.concurrent.FutureTask.run(FutureTask.java:138)

at javax.swing.SwingWorker.run(SwingWorker.java:319)

at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)

at java.lang.Thread.run(Thread.java:619)

[2104 byte] By [jdk2006a] at [2007-11-27 9:21:39]
# 1

Have you tried wrapping your

while (true) {

//various code

_robot.delay(6000)

loop with a try / catch(InterruptedException) block?

I'm not sure if that is all you need, but it might help. If anyone else has any ideas or finds fault with this, please let me know.

Message was edited by:

petes1234

petes1234a at 2007-7-12 22:15:35 > top of Java-index,Java Essentials,Java Programming...
# 2

Another thought (and again, I don't know if this is the kosher way or not), but instead of doing while (true) could you do this?

worker = new SwingWorker<Matrix, Void>() {

public Matrix doInBackground() {

while (!isCancelled()) {

//various code

_robot.delay(6000)

}

}

};

Addendum: nah, nevermind. You will be probably interrupting the _robot's delay regardless.

Message was edited by:

petes1234

petes1234a at 2007-7-12 22:15:35 > top of Java-index,Java Essentials,Java Programming...
# 3
Still not working.
jdk2006a at 2007-7-12 22:15:35 > top of Java-index,Java Essentials,Java Programming...
# 4
On the contrary: it is working perfectly. You cancelled the worker, which interrupted the thread that is running it. What did you expect?
ejpa at 2007-7-12 22:15:35 > top of Java-index,Java Essentials,Java Programming...
# 5

What about doing this?:

public void actionPerformed(ActionEvent ev) {

worker.cancel(false);

//System.exit(0);

}

petes1234a at 2007-7-12 22:15:35 > top of Java-index,Java Essentials,Java Programming...