Results 1 to 7 of 7
Thread: kill a thread
- 06-06-2007, 03:15 PM #1
Senior Member
- Join Date
- Jun 2007
- Posts
- 119
- Rep Power
- 0
- 06-07-2007, 01:45 PM #2
Senior Member
- Join Date
- Jun 2007
- Posts
- 164
- Rep Power
- 6
When I want to kill a thread I create a method that when I call it, it prepares the thread to kill it
check this code, and if you have doubts, tell me
byeJava Code:boolean finish = false; void run(){ while(!finish ){ //process } //clean resources } public void finish Thread(){ this.finish = true; }
- 07-02-2007, 06:55 PM #3
Senior Member
- Join Date
- Jun 2007
- Posts
- 110
- Rep Power
- 0
check if it is useful to you:
Java Code:class Example implements Runnable { Thread t; Object runLock = new Object(); volatile boolean shouldRun = false; public void start() { if (t == null) { shouldRun = true; t = new Thread(this); t.start(); } else { synchronized(runLock) { shouldRun = true; runLock.notify(); } } } // end start public void stop() { shouldRun = false; } // end stop public void run() { for (;;) { synchronized(runLock) { while (shouldRun == false) try { runLock.wait(); } catch (InterruptedException ie) { } } } } // end run }
- 08-03-2009, 09:41 PM #4
Member
- Join Date
- Aug 2009
- Location
- Kharkov, Ukraine
- Posts
- 29
- Rep Power
- 0
You can call Thread.interrupt(). But this does not mean that the thread will stop simultaneously. Read javadoc about how to use it.
- 06-19-2010, 05:25 AM #5
Member
- Join Date
- Jun 2010
- Posts
- 1
- Rep Power
- 0
how can i call finish method ?in the explanation of Heather
- 06-19-2010, 06:12 AM #6
That wasn't the point of his post. He's trying to say that you should make a method that sets a variable (let's call this variable stopNow) true while the thread you want to stop is running. In that thread, have all the process in a while loop that checks if stopNow is true or false. If it's true, get out of the while loop; thus, the process is terminated. However, the code has to be iterative.
"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 06-22-2010, 08:08 AM #7
the run method can have this structure
Java Code:public void run() { while(!isInterrupted()) { // doStuff try { } catch (InterruptedException ex) { interrupt(); } }
now, when you instantiate your thread and start it it will runs for ever until you call the method interrup() on your instance. this will cause the InterrupedException so that inside the run-method the catch-block will be executed and the interrupt() inside the catch-block will stop the while loop! a small example
Java Code:public class RunningThread implements Runnable { long start = System.currentTimeMillis(); public void run() { while (!Thread.currentThread().isInterrupted()) { ; try { Thread.sleep(10); } catch (InterruptedException ex) { System.out.println("RunningThread InterruptedException"); System.out.println("running time in millis: " + (System.currentTimeMillis() - start)); Thread.currentThread().interrupt(); } } } public static void main(String[] args) { RunningThread rt = new RunningThread(); Thread t = new Thread(rt); // start the RunnableThread, // set the current main thread to sleep for 2 secs or 2000 millis // and then stop the RunnableThread t.start(); try { Thread.sleep(2000); t.interrupt(); } catch (InterruptedException ex) { } } }Last edited by j2me64; 06-22-2010 at 08:16 AM.
Similar Threads
-
data from the main/GUI thread to another runnin thread...
By cornercuttin in forum Threads and SynchronizationReplies: 2Last Post: 04-23-2008, 10:30 PM -
How to get thread name
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:40 PM -
If JNI thread call the java object in another thread, it will crash.
By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 07:02 AM -
using Thread
By one198 in forum New To JavaReplies: 1Last Post: 11-21-2007, 08:01 AM


LinkBack URL
About LinkBacks


Bookmarks