SwingWorker

A

Alex

I've create a thread using SwingWorker but when I call the interrupt()
method it doesn't get interrupted.
I've checked the execution path and t.interrupt() DOES get executed in the
SwingWorker interrupt() method shown below.

public void interrupt() {
Thread t = threadVar.get();
if (t != null) {
t.interrupt();
}
threadVar.clear();
}
 
C

Chris Smith

Alex said:
I've create a thread using SwingWorker but when I call the interrupt()
method it doesn't get interrupted.
I've checked the execution path and t.interrupt() DOES get executed in the
SwingWorker interrupt() method shown below.

What are you expecting to happen when interrupt() is called? What will
actually happen is that the thread will be interrupted during its next
interruptible call (such as Thread.sleep or Object.wait), but will
continue to run until then. Also, it can check for interruption by
polling the Thread.interrupted() method.

If you're expecting that the thread will be immediately interrupted from
whatever it is doing (even if that's not blocking on an interruptible
call), then it's your expectation rather than the behavior that's wrong.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
A

Alex

Thanks, I understand now.

Chris Smith said:
What are you expecting to happen when interrupt() is called? What will
actually happen is that the thread will be interrupted during its next
interruptible call (such as Thread.sleep or Object.wait), but will
continue to run until then. Also, it can check for interruption by
polling the Thread.interrupted() method.

If you're expecting that the thread will be immediately interrupted from
whatever it is doing (even if that's not blocking on an interruptible
call), then it's your expectation rather than the behavior that's wrong.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top