?
Solved

Thread.currentThread().interrupt();

Posted on 2012-03-14
3
Medium Priority
?
544 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 2000 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 2000 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

Independent Software Vendors: 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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

770 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