InterruptedException - what to do with it ?

B

Ben_

Hello,

I've not found myself very lucky in finding a good article telling what to
do with the InterruptedException...

I'd like is read some discussions of best coding practices on that
particular field.

Many pieces of code (and not only samples) read like this:
(1)
try {
Thread.sleep(500);
} catch ( InterruptedException e ) {
// Variations of comments like "don't care", "do nothing"
// or log meaningless message to System.out, etc
}

A variation seen consist in wrapping and re-throwing the exception:
(2)
try {
Thread.sleep(500);
} catch ( InterruptedException e ) {
throw new RuntimeException(e);
}

Another approach seen consists in adding a "throws InterruptedException" to
the method signature:
(3)
public void doSomething() throws InterruptedException {}

In the case of a run() method, I can understand that nothing particular
needs to be done:
public void run() {
try {
while(!Thread.interrupted()) {
Thread.sleep(500);
// do something
}
} catch(InterruptedException e) {
// OK to terminate this way.
}
}

But for other cases, where a piece of code has to simply wait for some time
before doing something ?

Thaughts or pointers appreciated.

Thx.
 
O

Oliver Wong

Ben_ said:
Hello,

I've not found myself very lucky in finding a good article telling what to
do with the InterruptedException...

I'd like is read some discussions of best coding practices on that
particular field.

Many pieces of code (and not only samples) read like this:
(1)
try {
Thread.sleep(500);
} catch ( InterruptedException e ) {
// Variations of comments like "don't care", "do nothing"
// or log meaningless message to System.out, etc
}

A variation seen consist in wrapping and re-throwing the exception:
(2)
try {
Thread.sleep(500);
} catch ( InterruptedException e ) {
throw new RuntimeException(e);
}

Another approach seen consists in adding a "throws InterruptedException"
to
the method signature:
(3)
public void doSomething() throws InterruptedException {}

In the case of a run() method, I can understand that nothing particular
needs to be done:
public void run() {
try {
while(!Thread.interrupted()) {
Thread.sleep(500);
// do something
}
} catch(InterruptedException e) {
// OK to terminate this way.
}
}

But for other cases, where a piece of code has to simply wait for some
time
before doing something ?

Thaughts or pointers appreciated.

What one should do with an exception depends on what one is trying to
do. For example, in responce to a FileNotFoundException, you might just give
an error message and quit (if you're writing an game engine, and could not
load a vital resource, like the 3D Model of a monster), or you might tell
the user the specified file doesn't exist and pop up a FileOpenDialog.

In case where your program must wait at least a certain amount of time,
you should probably store the time that they can proceed, and then sleep for
a period equal to the that time minus the current time. If your sleeping
gets interrupted, just go back to sleep.

<pseudoCode>
timeToWakeUp = now + 5 minutes;
while (now < timeToWakeUp) {
try {
sleep(timeToWakeUp - now);
catch (InterruptedException e) {
continue; /*Go back to sleep*/
}
}
assert now >= timeToWakeUp;
doSomething();
</pseudoCode>

- Oliver
 
U

Ugo Matrangolo

Hi Ben,

Handling InterruptedException while your thread is waiting/sleeping
depends heavily on waht you are trying to do in your code.

Usually, an InterruptedException is catched when your thread waits for
a specific amount of time or is waiting on some conditions.

The first case was clearly explained by Oliver.

The second case can be seen as:

while (!condition) {
try {
wait();
} catch (InterruptedException ie) {
// you can do someting here but it is not needed
}
}

The important thing to note is that you can exit the wait() only if the
condition is meet. This kind of code must be present every time you
develop concurrent applications (es. the condition is a lock on
something.)

Regards,
Ugo.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top