Why invoke Thread.currentThread.interrupt() when catch any InterruptException?

Publish Your Own Wildly Successful and Profitable Newsletter Without Writing One Single Word Yourself & Become A Recognized Expert In The Internet Marketing Field Almost Effortlessly Get it now!

Up vote 7 down vote favorite 2 share g+ share fb share tw.

Java multithreading interrupt link|improve this question asked Feb 5 '11 at 12:25Hesey28418 65% accept rate.

This is done to keep state. When you catch the InterruptException and swallow it, you essentially prevent any higher level methods/thread groups from noticing the interrupt. Which may cause problems.

By calling Thread.currentThread.interrupt(), you set the interrupt flag of the thread, so higher level interrupt handlers will notice it and can handle it appropriately. Java Concurrency in Practice discusses this in more detail in Chapter 7.1.3: Responding to Interruption. Its rule is: Only code that implements a thread's interruption policy may swallow an interruption request.

General-purpose task and library code should never swallow interruption requests.

This article from the JavaSpecialist newsletter (which I strongly recommend) describes this practise clearly.

For this technique to work, it's critical that any method that catches an interrupt exception and is not prepared to deal with it immediately reasserts the exception. We say reasserts rather than rethrows, because it is not always possible to rethrow the exception. If the method that catches the InterruptedException is not declared to throw this (checked) exception, then it should "reinterrupt itself" with the following incantation: Thread.currentThread().interrupt(); This ensures that the Thread will reraise the InterruptedException as soon as it is able.

I would consider it a bad practice or at least a bit risky. Usually higher level methods do not perform blocking operations and they will never see InterruptedException there. If you mask it in every place you perform interruptible operation, you will never get it.

The only rationale for Thread.currentThread.interrupt() and not raising any other exception or signaling interrupt request in any other way (e.g. Setting interrupted local variable variable in a thread's main loop) is the situation where you really can't do anything with the exception, like in the finally blocks. See Péter Török's answer, if you want to better understand implications of the Thread.currentThread.interrupt() call.

I think this code sample makes things a bit clear. The class which does the job : public class InterruptedSleepingThread extends Thread { @Override public void run() { doAPseudoHeavyWeightJob(); } private void doAPseudoHeavyWeightJob() { for (int i=0;i if(Thread.currentThread().isInterrupted()) { System.out. Println("Thread interrupted\n Exiting..."); break; }else { sleepBabySleep(); } } } /** * */ protected void sleepBabySleep() { try { Thread.

Sleep(1000); } catch (InterruptedException e) { //e.printStackTrace(); Thread.currentThread().interrupt(); } } } The Main Class : public class InterruptedSleepingThreadMain { /** * @param args * @throws InterruptedException */ public static void main(String args) throws InterruptedException { InterruptedSleepingThread thread = new InterruptedSleepingThread(); thread.start(); //Giving 10 seconds to finish the job. Thread. Sleep(10000); //Let me interrupt thread.interrupt(); } } Try calling interrupt without setting the status back.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions