Results 1 to 4 of 4
  1. #1
    charlatain is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default 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?

  2. #2
    charlatain is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    Never mind! It seems liek the problem is solved. :)

  3. #3
    charlatain is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    No, it seems the problem did not fix itself. :(

  4. #4
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    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.

Similar Threads

  1. Escape Sequence
    By Punter in forum New To Java
    Replies: 4
    Last Post: 02-10-2009, 07:04 AM
  2. Escape Sequence \t does not display.
    By dreamingofgreen in forum New To Java
    Replies: 7
    Last Post: 07-22-2008, 04:50 AM
  3. Using escape sequence with like clause (%)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-14-2008, 09:57 AM
  4. Using escape sequence with like clause (_)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-14-2008, 09:55 AM
  5. Escape sequence problem
    By eva in forum New To Java
    Replies: 2
    Last Post: 01-21-2008, 10:15 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
  •