Solved

Thread.currentThread().interrupt();

Posted on 2012-03-14
3
521 Views
Last Modified: 2012-08-13
Hi guys

public class TaskRunnable impliments Runnable{
BlockingQueue<Task> queue;

public void run(){
try{
processTask(queue));
}
catch (InterruptedException e){
Thread.currentThread().interrupt(); -- What does this do and why is this considered a good practice?
}
}
}

thanks
0
Comment
Question by:royjayd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 36

Assisted Solution

by:mccarl
mccarl earned 500 total points
ID: 37723453
Check out the following link (especially the part titled 'Don't swallow interrupts'). It gives you a good idea about these things.
0
 

Author Comment

by:royjayd
ID: 37763946
I am having little hard time understanding this section from the link

"If you catch InterruptedException but cannot rethrow it, you should preserve evidence that the interruption occurred so that code higher up on the call stack can learn of the interruption and respond to it if it wants to. This task is accomplished by calling interrupt() to "reinterrupt" the current thread, as shown in Listing 3."

>>>so that code higher up on the call stack can learn of the interruption and respond to it if it wants to.

what is the code higher up the author is referring to ?
how does the code higher up respond to the interruption?

A simple example would be greatly appreciated.
thanks
0
 
LVL 36

Accepted Solution

by:
mccarl earned 500 total points
ID: 37764183
>> what is the code higher up the author is referring to ?

In the context of the code snippet that you posted in the original question, the "code higher up" would be the code that is running that particular runnable, ie. possibly it is a TaskExecutor, maybe a simple Thread or some other unknown custom implementation.

>> how does the code higher up respond to the interruption?

It could do anything it wants. What it can't do though, is to respond to an interruption when it has no way of knowing that there was an interruption.


>> A simple example would be greatly appreciated.

Hard to give because, well you questions to asked, highlight the issue. "What is the code" and "how does it respond"; the code could be anything and it could respond in anyway. The point is, that "you don't know" the answers to those questions (in most cases at least) and therefore you want to do your best to let this "unknown to you" piece of code know that something happened, and the way that you do this is by setting the interrupted flag, if you can't throw/rethrow the InterruptedException.

In the context of your original post, you are bound by a contract, the Runnable interface, that says that the run() method does NOT throw any checked exceptions (of which InterruptedException is one) and so the only way left to communicate the state of the program is to set the interrupted flag.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question