aspose file tools
The moose likes Threads and Synchronization and the fly likes how to stop a  running thread Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "how to stop a  running thread " Watch "how to stop a  running thread " New topic
Author

how to stop a running thread

zhengyw zhengyw
Greenhorn

Joined: Jul 08, 2003
Posts: 2
in my project,i use thread to dispatch my request to the service in my contorller,and the process is synchronized,that is ,in my thread's run() body,call a service's method,maybe the method last for a long time,then the user of client application maybe cancel this request,so in the executing of a thread,how can i stop it and then return the thread to a vector?thanks
Pavel Halas
Ranch Hand

Joined: Jul 05, 2003
Posts: 35
If I understand you, you would make the thread to stop using some user action (button click)...
You should create a boolean field that should be periodically checked. If the action is long-lasting, this could be combined with a process indication.
zhengyw zhengyw
Greenhorn

Joined: Jul 08, 2003
Posts: 2
yeah,i want to do that,but i how can i implement that idea?may i have to check a boolean field in the every running code line of a thread?
Jason Kosowan
Greenhorn

Joined: Sep 25, 2001
Posts: 9
Hi guys
Actually, there are mechanisms built into Java to do this kind of thing.
To stop a running thread, the class that stops the thread must call

But, this alone is not enough. Within the thread, I've successfully used the following in a method that is declared to throw InterruptedException.

Interspersing these calls at appropriate times within the thread (i.e. between critical operations) will give a responsive and safe way to stop and start things.
If you want to see this in action, my little open-source project Spiderfish uses this type of interruption with Swing/JFC.
Spiderfish Project
Hope this helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to stop a running thread
 
Similar Threads
when will a thread stop
How to kill a user thread forcefully?
Q on thread at Jxam
thread
how to kill main thread and relative subthread?