[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Thread.currentThread().interrupt();

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

649 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