From 3517096603e19cebe6b9402f54022d7fbbde03cd Mon Sep 17 00:00:00 2001 From: Hang Shao Date: Tue, 29 Nov 2022 14:24:45 -0500 Subject: [PATCH] Add synchronized block in getAndClearInterrupt() Fixes: https://github.com/eclipse-openj9/openj9/issues/15465 Port of https://github.com/ibmruntimes/openj9-openjdk-jdk19/pull/51 Signed-off-by: Hang Shao --- src/java.base/share/classes/java/lang/Thread.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/java.base/share/classes/java/lang/Thread.java b/src/java.base/share/classes/java/lang/Thread.java index c3ac62207cf..f51cc207414 100644 --- a/src/java.base/share/classes/java/lang/Thread.java +++ b/src/java.base/share/classes/java/lang/Thread.java @@ -1756,15 +1756,14 @@ final void clearInterrupt() { } boolean getAndClearInterrupt() { - boolean oldValue = interrupted; - // We may have been interrupted the moment after we read the field, - // so only clear the field if we saw that it was set and will return - // true; otherwise we could lose an interrupt. - if (oldValue) { - interrupted = false; - clearInterruptEvent(); + synchronized (interruptLock) { + boolean oldValue = interrupted; + if (oldValue) { + interrupted = false; + clearInterruptEvent(); + } + return oldValue; } - return oldValue; } /**