Results 1 to 3 of 3
- 04-23-2010, 05:47 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 12
- Rep Power
- 0
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 :
my stop 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(); } } } }
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.
- 04-23-2010, 07:10 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
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
- 04-24-2010, 03:09 AM #3
Member
- Join Date
- Apr 2010
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
How to stop Thread
By ersachinjain in forum Threads and SynchronizationReplies: 2Last Post: 11-30-2009, 07:11 PM -
Thread won't stop
By bubbless in forum Threads and SynchronizationReplies: 15Last Post: 10-20-2009, 10:58 PM -
Help to stop a thread
By raghu_lzybns in forum New To JavaReplies: 4Last Post: 07-09-2009, 04:39 PM -
how to stop a thread
By willemjav in forum Advanced JavaReplies: 19Last Post: 09-10-2008, 07:11 AM -
The safe way to stop a thread
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:31 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks