From b7e964ac6a543b8c3bffbcc8b6122f88ff1fc9e7 Mon Sep 17 00:00:00 2001 From: Hang Shao Date: Wed, 23 Nov 2022 11:28:32 -0500 Subject: [PATCH] Add synchronized block in getAndClearInterrupt() Fixes: https://github.com/eclipse-openj9/openj9/issues/15465 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 f7c7b524b6c..a8cfa9849d4 100644 --- a/src/java.base/share/classes/java/lang/Thread.java +++ b/src/java.base/share/classes/java/lang/Thread.java @@ -1813,15 +1813,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; } /**