Results 1 to 4 of 4
- 03-23-2009, 06:25 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 3
- Rep Power
- 0
Escape Loop with interrupted() call
I have two Threads, one called Timer, the other called TypingThread. After so-so minutes, Timer thread INTERRUPTS TypingThread. TypingThread's run() method consists of an infinite loop. To escape the loop when Timer interrupts TypingThread, I made an if statement as so:
if (Thread.interrupted == true) {
break;
from the loop. This does not work! The loop continues and starts a new iteration. How can I escape this loop?
- 03-23-2009, 06:29 AM #2
Member
- Join Date
- Mar 2009
- Posts
- 3
- Rep Power
- 0
Never mind! It seems liek the problem is solved. :)
- 03-23-2009, 06:32 AM #3
Member
- Join Date
- Mar 2009
- Posts
- 3
- Rep Power
- 0
No, it seems the problem did not fix itself. :(
- 03-23-2009, 06:52 AM #4
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 13
What you're doing ought to work, but:
- one thing that could be going wrong is if you're calling Thread.interrupted() somewhere inside your loop where you don't use that as a condition to exit the loop: calling Thread.interrupted() actually clears the thread's interrupted status.
- If you're stuck, you could always just use your own (volatile) boolean flag to signal the end of the loop. Unless you're using it for waking up from blocking calls such as Object.wait(), there's really nothing magic about Thread.interrupt().
In case you find something useful, I have a page about thread interruption that covers these and other points.Neil Coffey
Javamex - Java tutorials and performance info
Similar Threads
-
Escape Sequence
By Punter in forum New To JavaReplies: 4Last Post: 02-10-2009, 08:04 AM -
Escape Sequence \t does not display.
By dreamingofgreen in forum New To JavaReplies: 7Last Post: 07-22-2008, 05:50 AM -
Using escape sequence with like clause (%)
By Java Tip in forum Java TipReplies: 0Last Post: 02-14-2008, 10:57 AM -
Using escape sequence with like clause (_)
By Java Tip in forum Java TipReplies: 0Last Post: 02-14-2008, 10:55 AM -
Escape sequence problem
By eva in forum New To JavaReplies: 2Last Post: 01-21-2008, 11:15 PM
Bookmarks