Solved

Thread.currentThread().interrupt();

Posted on 2012-03-14
3
513 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
  • 2
3 Comments
 
LVL 35

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 35

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

696 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