Terminating threads - special case
I'm trying to modify some code to avoid using the deprecated stop() method associated to Thread. Essentially, a method from a blackbox class is executed in the thread. Since the computation can be very time-consuming, the user has the option of aborting computation via a button in the interface. I've tried using Thread.interrupt(), but have been unsuccessful (I've looked at the api doc). If anyone can shed some light on this, I'd be very grateful! Here are the relevant pieces of code:
In the calling class, I use the following methods:
private void process() {
filteringThread = new FilteringThread(detector);
filteringThread.start();
}
public synchronized void actionPerformed(ActionEvent e) {
if (e.getSource() == abortButton) {
if (filteringThread.isAlive()) {
filteringThread.stop();
}
}
}
This is the thread around the blackbox 'Detector' class:
class FilteringThread extends Thread {
private Detector detector;
public FilteringThread(Detector detector) {
this.detector = detector;
}
public void run() {
detector.execute();
}
}
Substituting interrupt() for stop() obviously does not work, and I can't test for interrupts inside Detector. The code above works perfectly, i.e. it does exactly what I want. Many thanks in advance for any input.
[1403 byte] By [
f_4a] at [2007-11-27 2:56:34]

Hi F4,
One of the big reasons stop() is deprecated because it is ...uncontrolled. There is no way to make sure the thread nicely cleans up after itself, closes data base connections, finishes persisting its state etc. We should instead cook up some nice methods where programmer makes sure the thread does a clean up and exits its run etc..
Having said all that, your Detector is a black box, perhaps some proprietary code that you don't license to change or extend?
I am thinking you have some pretty limited options!
1) Grey box. Are you sure there are no hooks into the API you can use? Is there a company who supplied it that might be able to give you ideas? (Assuming we are talking about some type of commercial arrangement.)
2) Edit black box. Do you have anything in place where you can get the owner of Detector to put some hooks in there for you? (Again, assuming we are talking about some type of commercial arrangement.)
3) Stop it anyway - if it is safe to do so. If it is safe to stop the thing in its tracks, that's obviously the easiest way to do it. This can be a tricky question though. I would ask myself things like:
- does it access a database?
- does it write to files?
- does it call any remote components?
- does it do anything involving money?
If the answer to any questions like this is "oh, ******, it does, yes" then I wouldn't use stop.
Not really answers I know, just more questions.
Rob
:)
Thanks for your answer/questions Rob,
Detector is a blackbox written by me. Essentially is contains several methods (chosen depending on input parameters) that each contain some fairly time-consuming loops. What kind of hooks are you talking about? I know I should do something at each passage in the loop, but I'm not sure what. I definitely don't want the thread to wait or sleep (the methods are time consuming, and every millisec counts :-).
Concerning 3), I guess stopping the thread is perfectly safe for my application - I just want to learn how to do this correctly.
f_4
f_4a at 2007-7-12 3:34:25 >
