Results 1 to 6 of 6
Thread: Stopping a running thread
- 12-28-2011, 02:04 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
- 12-28-2011, 02:09 PM #2
Re: Stopping a running thread
The running thread could test a flag and exit.
-
Re: Stopping a running thread
Here is an example for you
from: http://forward.com.au/javaProgrammin...opAThread.htmlJava Code:private volatile Thread myThread; public void stopMyThread() { Thread tmpThread = myThread; myThread = null; if (tmpThread != null) { tmpThread.interrupt(); } } public void run() { if (myThread == null) { return; // stopped before started. } try { // all the run() method's code goes here ... // do some work Thread.yield(); // let another thread have some time perhaps to stop this one. if (Thread.currentThread().isInterrupted()) { throw new InterruptedException("Stopped by ifInterruptedStop()"); } // do some more work ... } catch (Throwable t) { // log/handle all errors here } }
- 12-28-2011, 02:37 PM #4
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
Re: Stopping a running thread
i want to terminate its process before completing its task
-
Re: Stopping a running thread
In the link I gave you it argues that you should stop a thread with the interrupt()
- 12-28-2011, 02:43 PM #6
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
error - in one project is stopping other projects from running
By Sparky in forum NetBeansReplies: 2Last Post: 02-10-2011, 04:10 AM -
stopping the thread
By kailash in forum Threads and SynchronizationReplies: 2Last Post: 02-06-2011, 01:21 PM -
Stopping a thread
By Arne in forum Threads and SynchronizationReplies: 9Last Post: 10-21-2010, 10:26 AM -
Stopping a thread
By userj2ee in forum New To JavaReplies: 3Last Post: 08-13-2010, 07:57 PM -
Stopping a running thread in a multithreaded environment
By SUSHMA50 in forum Advanced JavaReplies: 11Last Post: 01-26-2009, 12:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks