What is the best way to exit a Runnable thread immediately?

K

Ken Adams

Basically I implement the Runnable interface and in my run function I do
have a while loop that checks a condition to quit, but what if my run
function is long say calls a bunch of different methods. Do I have to wait
till all the methods finish. I want to be able to quit anytime in the
progress of these functions. I suppose I could check the condition between
each method call but that doesn't seem like a very good choice. Any
suggestions?


Thanks
 
C

Chris Smith

Ken said:
Basically I implement the Runnable interface and in my run function I do
have a while loop that checks a condition to quit, but what if my run
function is long say calls a bunch of different methods. Do I have to wait
till all the methods finish. I want to be able to quit anytime in the
progress of these functions. I suppose I could check the condition between
each method call but that doesn't seem like a very good choice. Any
suggestions?

Checking multiple times is about what you want to do, if you need to
check several times per loop iteration.

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

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

Ann

Chris Smith said:
Checking multiple times is about what you want to do, if you need to
check several times per loop iteration.
My understanding is that checking a flag is the best, but if there are
latency problems you might see what happens if you use this:
public void interrupt() {}
which is in the Thread class. Then your thread must have a handler.
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top