4

what is recommended to do if joining threads does not work?

        for (List t : threads) {
           try {
              t.join();
           } catch (InterruptedException e) {
              log.error("Thread " + t.getId() + " interrupted: " + e);
              // and now?
           }
         }

is it recommended to break then (what happens then with the other threads which are not joined yet?) or should you at least try to join the rest of the threads and then go on?

Thanks for advices!

==> Conclusion: You should try again to join the specific thread t or you should interrupt this specific thread t and go on.

     for (List t : threads) {
        try {
          t.join();
       } catch (InterruptedException e) {    
            try {
                // try once! again:
                t.join();
            } catch (InterruptedException ex) {
                // once again exception caught, so:
                t.interrupt();
            }
         }
       }

so what do you think about this solution? and is it correct to do "t.interrupt()" or should it be Thread.currentThread().interrupt(); ?

thanks! :-)

nano7
  • 2,455
  • 7
  • 35
  • 52
  • Depends on what you want to do... – m0skit0 Feb 23 '12 at 10:52
  • 1
    I think you are mistaken. As @aix is saying, if that exception is thrown, then its the current thread that has been interrupted. Not the thread `t` in this context. – Johannes Feb 23 '12 at 10:55
  • so anyway, what would you advice me to do? break the procedure? just go on with joining the next thread in the list? try again to join all threads in the list? try again just to join the specific thread t? Thanks :-) – nano7 Feb 23 '12 at 11:52

1 Answers1

2

You get an InterruptedException because some other thread interrupted this, the joining, thread and not because the join didn't "work". Quoted from the API documentation:

InterruptedException - if another thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.


I would advice you to rejoin the thread again, example:

for (List t : threads) {
    while (true) {
        try {
            t.join();
            break;
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            // ... and ignore the interrupt
        }
    }
}
dacwe
  • 43,066
  • 12
  • 116
  • 140
  • so in your opinion I should go through my list of threads again and join them? wouldnt it be enough just to retry to join the specific thread t and then go on with the others? in any case, you would not break the joining, true? – nano7 Feb 23 '12 at 11:50
  • 1
    sorry, for reasking: what exactly would be the good solution? join ALL threads again or just join the SPECIFIC thread again and go on then with the others? thanks :-) – nano7 Feb 23 '12 at 12:13
  • :-), sorry, rejoin only the one you got interrupted joining and continue joining the rest. – dacwe Feb 23 '12 at 12:18
  • thanks! again, in another post I read I should do this: Thread.currentThread().interrupt(); when I catch the exception and then go on. What do you think about this way to handle the exception? – nano7 Feb 23 '12 at 12:23
  • That is to keep the thread interrupt state. See this [SO post](http://stackoverflow.com/questions/4906799/why-invoke-thread-currentthread-interrupt-when-catch-any-interruptexception). So that is probably a good thing todo. – dacwe Feb 23 '12 at 12:34
  • I updated my posting with a possible solution. Would you take a look at it? Thanks! – nano7 Feb 23 '12 at 12:51
  • You might find interesting the implementation of Guava's [`Uninterruptibles.joinUninterruptibly(Thread)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/Uninterruptibles.html#joinUninterruptibly(java.lang.Thread)). – Louis Wasserman Feb 23 '12 at 16:14
  • 1
    Interrupting the thread again in the loop will cause join to throw an interrupt exception again and you produce a busy loop. Set a boolean indicating that the thread was interrupted, and interrupt the thread after the loop if the boolean is set. – Michael Krussel Feb 23 '12 at 17:26
  • @MichaelKrussel Maybe I misunderstand you, but not trying to join the thread again but to use "Thread.currentThread().interrupt()" (or t.interrupt()?) is then a solution in your opinion? maybe, to avoid misunderstanding, could you post your solution as well in an answer? that would be great - thank you! – nano7 Feb 24 '12 at 08:56