[concurrency-interest] Condition.awaitNanosUninterruptibly why it is missing?

Peter Veentjer alarmnummer at gmail.com
Tue Mar 6 04:17:15 EST 2007

I was wondering why the Condition.awaitNanosUninterruptibly(long)
method is missing. You are able to do an uninterruptible wait on the
Condition without a timeout, but you are not able to do an
uninterruptible wait on a Condition with a timeout.

I have made the following implementation to bypass this limitation and
was wondering if it is correct.

 public static long awaitNanosUninterruptiblyAndThrow(Condition
condition, long timeoutNs) throws TimeoutException {
        if (condition == null) throw new NullPointerException();

        if(timeoutNs<=0) throw new TimeoutException();

        //This call clears the interruptstatus from the thread, because
        //the waiting we are going to do can't be interrupted.
        //But the interrupt status is restored at the
        //end of this method.
        boolean interrupt = Thread.interrupted();
        try {
            long startNs;
            while (true) {
                startNs = System.nanoTime();
                try {
                    timeoutNs = condition.awaitNanos(timeoutNs);
                    if(timeoutNs <= 0) throw new TimeoutException();
                    return timeoutNs;
                } catch (InterruptedException e) {
                    //make sure that the interrupt status is restored
when the thread
                    //leaves this method.
                    interrupt = true;
                    timeoutNs -= System.nanoTime()-startNs;
                    if(timeoutNs<=0) throw new TimeoutException();
        }finally {
            //If the thread has the interrupt status when it started calling
            //this method, it status should be restored. If the interrupt status
            //was set while waiting, it remains set.
            if (interrupt)

More information about the Concurrency-interest mailing list