aspose file tools*
The moose likes Threads and Synchronization and the fly likes How can I really stop/kill a thread? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How can I really stop/kill a thread?" Watch "How can I really stop/kill a thread?" New topic
Author

How can I really stop/kill a thread?

Roy Huang
Ranch Hand

Joined: Nov 21, 2001
Posts: 104
I have currently the problem of using a timer thread to limit the HTTP connection.
This is a timer class extending the Thread class, In a synchronized run method, two counters will be compared for time out, the exception will be thrown and the other HTTP application thread will be stopped.
The problem in my case is that:
The HTTP application and this timer thread is one target in the anttask, if more targets are executed sequentially in this ant task, the timer thread always blocks other application--- since when the HTTP application is finished, it can not stop the timer thread correctly. (Here I use timerthread = null, as Java.Sun.com suggest not use the stop() method-> to stop the thread.)
so the exact http application is not blocked by this thread but some others will be affected..
Does anybody have some idea about it, thanks.
Roy


SCJP 1.4/SCBCD/SCWCD<br />MCSE<br />IBM professional XML Developer<br />SAP certified Development Consultant (NetWeaver portal and Web JavaApplication)
Doanh Nguyen
Ranch Hand

Joined: Dec 02, 2000
Posts: 45
Originally posted by Roy Huang:
...the exception will be thrown and the other HTTP application thread will be stopped.
...since when the HTTP application is finished, it can not stop the timer thread correctly.

I am confused. Do you want the timer to stop the HTTP application or the HTTP application to stop the timer?
suvasis mukherjee
Greenhorn

Joined: Feb 22, 2003
Posts: 5
hi,
I am not clear, but if you looking to stop a thread, can you try join().
another suggestion:
say:
public MyRunnable implements Runnable {
public void run() {
do_something();
}
}
Thread t = new Thread(new MyRunnable());
t.start()
The reason is:
You are probably not changing the nature of thread, so don't subclass it. In java Thread object is just an object that happens to have pointer to the actual thread (i mean stack, kernel, etc). Once a thread is exited, it's gone. It is not "restartable". Hence, runnable is better, you can have as many threads as you want....
so view Threads as the engines for getting work done, while the Runnable is the work to be done.
Originally posted by Roy Huang:
[QB]I have currently the problem of using a timer thread to limit the HTTP connection.
This is a timer class extending the Thread class, In a synchronized run method, two counters will be compared for time out, the exception will be thrown and the other HTTP application thread will be stopped.
The problem in my case is that:
The HTTP application and this timer thread is one target in the anttask, if more targets are executed sequentially in this ant task, the timer thread always blocks other application--- since when the HTTP application is finished, it can not stop the timer thread correctly. (Here I use timerthread = null, as Java.Sun.com suggest not use the stop() method-> to stop the thread.)
so the exact http application is not blocked by this thread but some others will be affected..
QUOTE]
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2861
    
  11

I don't think join() stops a thread. It just waits for it to die naturally.
I can't say I really understood the OP very well, but the way I stop a thread is to create my own kill() method. This method just sets a kill flag. Then in my run() method, I have to periodically check for the kill flag at safe places, and exit when it is set.
The problem with the stop() method is it could cause the thread to cease in the middle of a critical section of code, which could lead to objects being in an inconsistant state. By using a kill flag, you choose the point where the thread can die, and so (hopefully) don't corrupt anything.
Roy Huang
Ranch Hand

Joined: Nov 21, 2001
Posts: 104
Hi, tahnks for your opinions. I agree with Greg's words. The best way to stop a thread is not use join() or thread=null. But to write your own Program. The tipps what I gathered are:
1. Using "While" of "If" to detetc a flag which used to stop the thread in your run() methods.
2. If you want to stop another thread, then you can use "another thread = null" in your own stop method. If you just want to stop this thread, then just set the flag as true to jump out of the loop in run() methods.
Ex:
private volatile boolean flag=false;
private stop(){
Otherthread=null;
// or other methods to stop the application
}
public void run()
{
while(!flag)
{
try
{Thread.sleep(m_rate);}
catch (InterruptedException ioe)
{continue;
}

synchronized ( this )
{

m_elapsed += m_rate;

if (m_elapsed > m_length)
{
flag=true;
stop();

}
}
}
}
Here m_elapsed and m_length is two timer, you can set the m_length parameter in the thread constructor .
Roy
Mallick Srinivas Choppa
Greenhorn

Joined: Jan 20, 2003
Posts: 3
Hi,
The best way to stop the thread is use interrupt() of the Thread class.
Probably you may be waiting in the thread so whenever you call interrupt menthod the thread object it is caugth in the exception and then you can smoothly end the thread.
Hope this would help.
Mallick
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Interrupt only works if the thread is doing something "interruptable." Otherwise it will not do anything. Unless you are talking about overriding the built in interrupt methodology?
Roy Huang
Ranch Hand

Joined: Nov 21, 2001
Posts: 104
Thanky for the repsonse, in my case, I use a http application which I can easily use disclose() method to stop this thread. It really depends on each situation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I really stop/kill a thread?