Results 1 to 10 of 10
Thread: Stopping a thread
- 09-02-2010, 02:01 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
- 09-02-2010, 03:22 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 09-02-2010, 03:27 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
the thread which i want to stop doesnt interact with any other thread. it only calculates some values and returns them afterwards. can i use stop() then safely
- 09-02-2010, 03:31 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 09-02-2010, 04:07 PM #5
i found a small example how to stop a thread. perhaps you can use it for your problem. here is the code:
Java Code:class Thread1 extends Thread { public void run() { int i = 0; System.out.println("while-loop starting ..."); while (!isInterrupted()) { System.out.println(i++); try { Thread.sleep(50); } catch (InterruptedException e) { System.out.println("interrupt received"); interrupt(); } } System.out.println("while-loop ended"); } } public class MyThread { public static void main(String[] args) { long start = System.currentTimeMillis(); Thread1 t = new Thread1(); t.start(); try { Thread.sleep(2000); System.out.println("call interrupt"); } catch (InterruptedException e) { } t.interrupt(); System.out.println("main ended"); System.out.println("duration of all tasks in millis: " + (System.currentTimeMillis() - start)); } }
the thread is started inside the main function and also interrupted from the main function. in order to stop the thread you need this loop
while (!isInterrupted())
and inside the catch-block use the method interrupt() so that the boolean test in while becomes true and the thread ends. hope you understand the logic. good luck.
- 09-02-2010, 04:19 PM #6
The OPs problem is:
It appears that he doesn't have the source for the looping thread, so he can't change it as per all your recommendationsI cant use a variable to interrupt the while loop of the thread cause i cant edit this functions
- 09-03-2010, 11:27 AM #7
oh, norm, for somebody who pretends to implement threads changing my code to fulfill the requirements should be an easy task: so declare a instance variable ex. stop in the class Thread1 with the value false, change the while to "while (!stop)". now from outside the class you can modify the stop instance variable to true and the thread will stop. if you need this code let me know.Last edited by j2me64; 09-03-2010 at 11:30 AM.
- 09-03-2010, 11:59 AM #8
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
i dont have the source code of any functions. they are from a library. i cant modify any while loop or implement stop variables.
But thx for your help
- 10-21-2010, 10:15 AM #9
Spammer lmno947 reported
db
- 10-21-2010, 10:26 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Stopping a thread
By userj2ee in forum New To JavaReplies: 3Last Post: 08-13-2010, 07:57 PM -
Stopping a .swf with java
By ercarls in forum New To JavaReplies: 2Last Post: 04-14-2010, 06:33 PM -
stopping second time round
By silverspoon34 in forum New To JavaReplies: 0Last Post: 11-20-2009, 02:04 PM -
Stopping a running thread in a multithreaded environment
By SUSHMA50 in forum Advanced JavaReplies: 11Last Post: 01-26-2009, 12:22 AM -
stopping thread...using flags
By rstepler in forum New To JavaReplies: 1Last Post: 07-31-2008, 09:36 PM


LinkBack URL
About LinkBacks


Bookmarks