Results 1 to 10 of 10
  1. #1
    Arne is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default Stopping a thread

    Hi! I have some excisting functions from a library and want to run some of them in a single thread. My Problem is: How can i stop this threads. I cant use a variable to interrupt the while loop of the thread cause i cant edit this functions

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Arne View Post
    Hi! I have some excisting functions from a library and want to run some of them in a single thread. My Problem is: How can i stop this threads. I cant use a variable to interrupt the while loop of the thread cause i cant edit this functions
    If you want to stop a Thread that other Thread has to cooperate in some way; if it doesn't, you're out of luck. The Thread.stop() method is deprecated for good reasons.

    kind regards,

    Jos

  3. #3
    Arne is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    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

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Arne View Post
    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
    Try it; I don't know; if that Thread uses resources somewhere if might not be safe. If it just uses some memory for its stack and objects it might work. Read what the API documentation has to say about that method ...

    kind regards,

    Jos

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    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.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    The OPs problem is:
    I cant use a variable to interrupt the while loop of the thread cause i cant edit this functions
    It appears that he doesn't have the source for the looping thread, so he can't change it as per all your recommendations

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    It appears that he doesn't have the source for the looping thread, so he can't change it as per all your recommendations

    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.

  8. #8
    Arne is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    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

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Spammer lmno947 reported

    db

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Spammer lmno947 reported
    ditto; the moron is all over the place at the moment (and has been here before).

    kind regards,

    Jos

Similar Threads

  1. Stopping a thread
    By userj2ee in forum New To Java
    Replies: 3
    Last Post: 08-13-2010, 07:57 PM
  2. Stopping a .swf with java
    By ercarls in forum New To Java
    Replies: 2
    Last Post: 04-14-2010, 06:33 PM
  3. stopping second time round
    By silverspoon34 in forum New To Java
    Replies: 0
    Last Post: 11-20-2009, 02:04 PM
  4. Replies: 11
    Last Post: 01-26-2009, 12:22 AM
  5. stopping thread...using flags
    By rstepler in forum New To Java
    Replies: 1
    Last Post: 07-31-2008, 09:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •