dcsimg
Stopping threads with Java2
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Wednesday, March 28, 2001 02:42 PM

Hello! I'm writing an application which uses threads. In short there is a point where a proccess that launches two threads need to stop one of them (for instance because the other one has finished). I'm trying to avoid the use of the method stop() because it's deprecated. However, the solution given by Sun in its API ("Why stop() [...] where deprecated") consists of a kind of pooling I think it's not very smart. [1] They propose to check periodically a variable in order to see if it's been changed by a process that wants to stop the thread. [2] What I tried to do is: 2.1) At the run() method: try { /* code */ } catch (InterruptedException e) {    More>>

Hello!

I'm writing an application which uses threads.

In short there is a point where a proccess that launches two threads need to stop one of them (for instance because the other one has finished).

I'm trying to avoid the use of the method stop() because it's deprecated. However, the solution given by Sun in its API ("Why stop() [...] where deprecated") consists of a kind of pooling I think it's not very smart.

[1] They propose to check periodically a variable in order to see if it's been changed by a process that wants to stop the thread.

[2] What I tried to do is:

2.1) At the run() method:

try {
/* code */
} catch (InterruptedException e) {
}

so, when the thread is interrupted it finishes.

2.2) To interrupt the thread.

But the problem is that the external interruption (call to interrupt()) does not throws an InterruptedException.
Instead of that interruption it simply interrupts the threads execution (run() method), jumps to the interrupt() method and returns.

Another "problem" is that /* code */ needs to throw an InterruptedException. If not the try {} catch() {} block is erroneous.

[3] What I need is as kind of asynchronous signal that reaches the run() method (and can be captured by it).

I've also thought of throwing InterruptedException at the interrupt() method (I haven't tried it) but I think it will throw the exception not at the method interrupted but at the point of code from where you did the call to interrupt().

I was also told to use PropertyChangeListeners but I think I am in the same situation. I can capture this kind of asynchronous signal but not at the run method (because it doesn't throw an exception but does a call to a method).

Do you know a smart solution to this?

Thanks in advance!

   <<Less

Re: Stopping threads with Java2

Posted By:   Chilang_Shen  
Posted On:   Monday, April 9, 2001 09:47 PM

Hello, I always stop a thread via the following way:



Construct a Runnable class instead of extends from Thread, like this:

public class MyThread implements Runnable{

boolean run_f=false;

Thread thisThread = null;

// ... your code

public synchronized void start(){

if (!run_f){

run_f = true;

thisThread = new Thread(this);

thisThread.start();

}

}



public synchronized void stop(){

if (run_f){

run_f = false;

thisThread = null;

}

}



public void run(){

while(run_f){

try{

do_process();

}catch(InterruptedException e){

}

}

}

}



then, when you invoke stop() the thread will stop (after a loop?).



Can it help you?

Good luck my friends!!!

Re: Stopping threads with Java2

Posted By:   Christopher_Schultz  
Posted On:   Wednesday, April 4, 2001 02:16 PM

The best solution is to use the method Sun recommends.



If you have your run() method mostly looping:




while(!finished)
{
/* do something */
}



... there's no reason not to add && !stopped to your loop condition. It's not considered busy-waiting or polling if you're actually doing something useful in your loop.



If your method is more linear, then add several statements like:




if(stopped) return;



... and wrap your method in a try{ ... } finally { ... } block in order to clean up your code no matter what happens.



-chris
About | Sitemap | Contact