[concurrency-interest] Propagation of signals to non-interrupted thread

Dr Heinz M. Kabutz heinz at javaspecialists.eu
Thu Nov 10 08:15:03 EST 2011


In Java 6, the ArrayBlockingQueue used this construct for the take() method:

    public E take() throws InterruptedException {
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            try {
                while (count == 0)
                    notEmpty.await();
            } catch (InterruptedException ie) {
                notEmpty.signal(); // propagate to non-interrupted thread
                throw ie;
            }
            E x = extract();
            return x;
        } finally {
            lock.unlock();
        }
    }

In other words, it would /always/ send a signal on interrupt, even if it 
had not received one.

In Java 7, this was taken away, so we now have:

    public E take() throws InterruptedException {
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            while (count == 0)
                notEmpty.await();
            return extract();
        } finally {
            lock.unlock();
        }
    }

However, I could not find substantial differences between the await() 
methods of Java 6 and 7.  Does this mean that propagating of the signal 
was not necessary in Java 6 either?  According to Doug Lea's book 
section 3.2.4.2, it is necessary with wait/notify to propagate the 
signal if you get interrupted.  However, it looks like Condition.await() 
is coded to cater for this eventuality anyway.

Regards

Heinz
-- 
Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun Java Champion
IEEE Certified Software Development Professional
http://www.javaspecialists.eu
Tel: +30 69 72 850 460
Skype: kabutz 



More information about the Concurrency-interest mailing list