Results 1 to 7 of 7

Thread: kill a thread

  1. #1
    Peter is offline Senior Member
    Join Date
    Jun 2007
    Posts
    119
    Rep Power
    0

    Default kill a thread

    How can I kill a thread? without using stop();
    please help me
    peter

  2. #2
    Heather is offline Senior Member
    Join Date
    Jun 2007
    Posts
    164
    Rep Power
    8

    Default

    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
    Java Code:
    boolean finish = false;
    void run(){
     while(!finish ){
         //process
     }
    //clean resources
    }
    public void finish Thread(){
     this.finish = true;
    }
    bye

  3. #3
    Ed
    Ed is offline Senior Member
    Join Date
    Jun 2007
    Posts
    110
    Rep Power
    0

    Default

    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
    
    
      }

  4. #4
    vkorenev is offline Member
    Join Date
    Aug 2009
    Location
    Kharkov, Ukraine
    Posts
    29
    Rep Power
    0

    Default

    You can call Thread.interrupt(). But this does not mean that the thread will stop simultaneously. Read javadoc about how to use it.

  5. #5
    rakeshvarma is offline Member
    Join Date
    Jun 2010
    Posts
    1
    Rep Power
    0

    Default

    how can i call finish method ?in the explanation of Heather

  6. #6
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    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)

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

    Default

    Quote Originally Posted by rakeshvarma View Post
    how can i call finish method ?in the explanation of Heather

    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

  1. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 10:30 PM
  2. How to get thread name
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:40 PM
  3. Replies: 0
    Last Post: 01-28-2008, 07:02 AM
  4. using Thread
    By one198 in forum New To Java
    Replies: 1
    Last Post: 11-21-2007, 08:01 AM

Posting Permissions

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