Reputation: 21319
My assumption is that LockSupport.parkNanos(long)
will not throw InterruptedException
, but the flag may be set on the thread.
InterruptedException
?Sample usage:
import java.util.concurrent.locks.LockSupport;
public void parkNanosInterruptibly(final long nanos)
throws InterruptedException {
LockSupport.parkNanos(nanos);
// If this thread was interrupted during parkNanos(), we must throw "by contract".
if (Thread.interrupted()) {
throw new InterruptedException();
}
}
Upvotes: 2
Views: 548
Reputation: 1030
Yes, it'll not throw InterruptedException. JavaDoc
public static void parkNanos(long nanos)
Disables the current thread for thread scheduling purposes, for up to the specified waiting time, unless the permit is available. If the permit is available then it is consumed and the call returns immediately; otherwise the current thread becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:
Some other thread invokes unpark with the current thread as the target;
or Some other thread interrupts the current thread;
or The specified waiting time elapses;
or The call spuriously (that is, for no reason) returns.
This method does not report which of these caused the method to return. Callers should re-check the conditions which caused the thread to park in the first place. Callers may also determine, for example, the interrupt status of the thread, or the elapsed time upon return.
Parameters: nanos - the maximum number of nanoseconds to wait
And yes, it's not correct to ignore the fact of interruption. So you have to check for interruption and somehow deal with it (e.g. close some recourses and throw exception or shutdown thread or whatever).
There is a good article from one of Java Language Architects Brian Goetz, http://www.ibm.com/developerworks/library/j-jtp05236/
Upvotes: 3