RFR: 8349543: LinkedBlockingDeque does not immediately throw InterrruptedException in put/take
kabutz
duke at openjdk.org
Mon Mar 10 18:39:59 UTC 2025
On Wed, 5 Feb 2025 15:36:15 GMT, kabutz <duke at openjdk.org> wrote:
> The LinkedBlockingDeque does not behave consistently with other concurrency components. If we call putFirst(), putLast(), takeFirst(), or takeLast() with a thread that is interrupted, it does not immediately throw an InterruptedException, the way that ArrayBlockingQueue and LInkedBlockingQueue does, because instead of lockInterruptibly(), we call lock(). It will only throw an InterruptedException if the queue is full (on put) or empty (on take). Since interruptions are frequently used as a shutdown mechanism, this might prevent code from ever shutting down.
Should we drop this PR or continue with it?
I noticed that the timed offer() and poll() methods are consistent with the way that LinkedBlockingQueue works, in other words, they throw the InterruptedException as soon as we enter the methods. They use lock.lockInterruptibly(), which I was suggesting we use for put() and take().
import java.util.concurrent.*;
public class OfferPollLBD {
public static void main(String... args) {
var lbd = new LinkedBlockingDeque<Integer>();
Thread.currentThread().interrupt();
System.out.println(lbd.offer(1)); // should work
System.out.println(lbd.add(2)); // should work
try {
System.out.println(lbd.offer(3, 10, TimeUnit.SECONDS)); // throws IE
} catch (InterruptedException e) {
System.out.println("Interrupted");
Thread.currentThread().interrupt();
}
try {
lbd.put(4); // IMHO should throw IE, but doesn't
System.out.println("put(4) succeeded");
} catch (InterruptedException e) {
System.out.println("Interrupted");
Thread.currentThread().interrupt();
}
System.out.println("lbd = " + lbd);
System.out.println(lbd.poll()); // should work
System.out.println(lbd.remove()); // should work
try {
System.out.println(lbd.poll(10, TimeUnit.SECONDS)); // throws IE
} catch (InterruptedException e) {
System.out.println("Interrupted");
Thread.currentThread().interrupt();
}
try {
System.out.println(lbd.take());
} catch (InterruptedException e) {
System.out.println("Interrupted");
Thread.currentThread().interrupt();
}
System.out.println("lbd = " + lbd);
}
}
-------------
PR Comment: https://git.openjdk.org/jdk/pull/23464#issuecomment-2711493965
More information about the core-libs-dev
mailing list