Results 1 to 3 of 3

Thread: Interrupts

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

    Default Interrupts

    I was reading the tutorial on Concurrency and I'm stumbled on this:

    Java Code:
    for (int i = 0; i < importantInfo.length; i++) {
        //Pause for 4 seconds
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            //We've been interrupted: no more messages.
           [b] return;[/b]    }
        //Print a message
        System.out.println(importantInfo[i]);
    }
    and this:

    Java Code:
    for (int i = 0; i < inputs.length; i++) {
        heavyCrunch(inputs[i]);
        if (Thread.interrupted()) {
            //We've been interrupted: no more crunching.
            return;
        }
    }
    this is the link: Interrupts (The Java™ Tutorials > Essential Classes > Concurrency)

    My question are:
    1. What does return; do in both codes?
    2. In code 2, why/how would a for loop be interrupted? Thread.interrupt() was never called.
    Last edited by Lil_Aziz1; 01-02-2010 at 06:56 PM.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    perhaps another thread would have a handle to this thread and would have called its thread.interrupt().

    interrupting a thread is the preferred way now to have one thread try to tell a second thread to stop running. Having the test for interrupted within the for loop of the first code sample around the thread sleep catches the interrupted exception that would get generated here if this thread was sleeping.

    but for the second code block, im not entirely sure, but I believe that Thread.interrupted() test works there on its own to check if the thread has been interrupted, such as by another thread calling its thread.interrupt(), and this would cause the loop to preemptively exist, instead of continuing to finish its for() loop iterations. The Thread.interrupted is a static method that implicitly looks at the interrupted flag on the current thread.


    In both cases the return causes the run() method in the thread to exit, which then would cause the thread to die. In theory without having these test for interrupted. the thread would continue to run until it finished naturally, ignoring the 'stop running' command from when the some other thread called thread.interrupt().

    .. but in this example, I would expect the first code block would also need a test for if (Thread.interrupted() ), because if it was not in the part of thread.sleep() at the time it was interrupted, would it miss the interrupt() signal, or would the next time the Thread.sleep() is invoked, would it throw an InterruptedException?

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,432
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by travishein View Post
    .. but in this example, I would expect the first code block would also need a test for if (Thread.interrupted() ), because if it was not in the part of thread.sleep() at the time it was interrupted, would it miss the interrupt() signal, or would the next time the Thread.sleep() is invoked, would it throw an InterruptedException?
    It would simply miss the signal but its 'interrupted' flag is set. A Thread can go to sleep when this flag has been set and an Exception will not be thrown so, indeed, it is safer to explicitly test whether or not the Thread was interrupted before.

    kind regards,

    Jos

Tags for this Thread

Posting Permissions

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