Java: if Threads connection does not work: interruption or continuation?

what is recommended to do if the connection of streams does not work?

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

Is it then recommended to interrupt (what happens then with other threads that are not yet merged?) or should you at least try to join the other threads and then continue?

Thanks for the tips!

==> Conclusion . You must try joining a specific thread t again, or you must abort this specific thread t and continue.

  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 of this decision? and whether it is correct to do "t.interrupt ()" or should there be Thread.currentThread (). interrupt ();

thanks!: -)

+4
source share
1 answer

You get an InterruptedException because some other thread interrupted this, connection, thread, and not because join did not work. Quoted from the API documentation :

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


I would advise you to rejoin the topic , for example:

 for (List t : threads) { while (true) { try { t.join(); break; } catch (InterruptedException e) { Thread.currentThread().interrupt(); // ... and ignore the interrupt } } } 
+2
source

Source: https://habr.com/ru/post/1398009/


All Articles