Solved

Thread.currentThread().interrupt();

Posted on 2012-03-14
3
503 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoreUp challenge 14 48
computer science syllabus 3 52
Understanding websocket example in spring 1 42
Python Assistance 7 35
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now