views:

229

answers:

1

I was looking at the source code of java.uti.concurrent.locks.AbstractQueuedSynchronizer and the acquire() method looks something like this -

 public final void acquire(int arg) {
    if (!tryAcquire(arg) &&
        acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
        Thread.currentThread().interrupt();
}

Why does it interrupt the thread calling acquire()? If there was check somewhere in the threads run() method, then it might pass after calling acquire() which is probably undesirable and unthought of?

Anybody care to shed light on why the above piece of code does this?

+4  A: 

If you read the Javadoc for acquiredQueued, you will noticed that it returns true if the thread was interrupted while waiting. Thus, the call to selfInterrupt (as it's called in the OpenJDK source code) is to propagate the interrupt to the calling thread, which would otherwise get swallowed.

Chris Jester-Young