Results 1 to 3 of 3
  1. #1
    luoluo is offline Member
    Join Date
    Apr 2010
    Posts
    12
    Rep Power
    0

    Default How to stop thread when it is sleeping

    Hi everyone,

    I have encounter a issue about thread,I start a thread,sleep 10 seconds to continue after it prints string.when I stop this thread to throw the following exception.

    java.lang.InterruptedException: sleep interrupted
    Test is sucessful.
    Test is sucessful.
    at java.lang.Thread.sleep(Native Method)
    at TestThread.run(TestThread.java:8)

    my thread code :

    Java Code:
    public class TestThread extends Thread {
    	public void run(){
    		while(true){
    			System.out.println("Test is sucessful.");
    			try {
    				Thread.sleep(10000);
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}
    }
    my stop code:

    Java Code:
    TestThread t = new TestThread();
    		t.start();
    
    		try {
    			Thread.sleep(5000);
    		} catch (InterruptedException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
    		if(t != null){
    			
    			t.interrupt();
    		}

    I want to stop it when it with in this situation,how to stop.

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    i'm not sure i understand your question fully, but if your concern is the exception, don't worry about it.

    when you call t.interrupt(), what it does is raises an InterruptedException inside thread t, specifically to let it know hat someone called interrrupt() on it. So simply don't print out the stack trace and you shoudl be good

  3. #3
    luoluo is offline Member
    Join Date
    Apr 2010
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by iluxa View Post
    i'm not sure i understand your question fully, but if your concern is the exception, don't worry about it.

    when you call t.interrupt(), what it does is raises an InterruptedException inside thread t, specifically to let it know hat someone called interrrupt() on it. So simply don't print out the stack trace and you shoudl be good
    because I have a sever to manage this thread,when it run or sleep,I want to send a request from client to server stop it immaditely.

Similar Threads

  1. How to stop Thread
    By ersachinjain in forum Threads and Synchronization
    Replies: 2
    Last Post: 11-30-2009, 08:11 PM
  2. Thread won't stop
    By bubbless in forum Threads and Synchronization
    Replies: 15
    Last Post: 10-20-2009, 11:58 PM
  3. Help to stop a thread
    By raghu_lzybns in forum New To Java
    Replies: 4
    Last Post: 07-09-2009, 05:39 PM
  4. how to stop a thread
    By willemjav in forum Advanced Java
    Replies: 19
    Last Post: 09-10-2008, 08:11 AM
  5. The safe way to stop a thread
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 07:31 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
  •