diff --git a/src/java.base/share/classes/java/lang/Thread.java b/src/java.base/share/classes/java/lang/Thread.java index f658400d378..fa683500470 100644 --- a/src/java.base/share/classes/java/lang/Thread.java +++ b/src/java.base/share/classes/java/lang/Thread.java @@ -1614,32 +1614,35 @@ void clearReferences() { * a chance to clean up before it actually exits. */ void exit() { - /* Refresh interrupted value so it is accurate when thread reference is removed. */ - interrupted = interrupted(); - try { - // pop any remaining scopes from the stack, this may block - if (headStackableScopes != null) { - StackableScope.popAll(); - } - } finally { - // notify container that thread is exiting - ThreadContainer container = threadContainer(); - if (container != null) { - container.onExit(this); + /* Refresh interrupted value so it is accurate when thread reference is removed. */ + interrupted = interrupted(); + + try { + // pop any remaining scopes from the stack, this may block + if (headStackableScopes != null) { + StackableScope.popAll(); + } + } finally { + // notify container that thread is exiting + ThreadContainer container = threadContainer(); + if (container != null) { + container.onExit(this); + } } - } - try { - if (threadLocals != null && TerminatingThreadLocal.REGISTRY.isPresent()) { - TerminatingThreadLocal.threadTerminated(); + try { + if (threadLocals != null && TerminatingThreadLocal.REGISTRY.isPresent()) { + TerminatingThreadLocal.threadTerminated(); + } + } finally { + clearReferences(); } } finally { - clearReferences(); - } - synchronized (interruptLock) { - // so that isAlive() can work - eetop = Thread.NO_REF; + synchronized (interruptLock) { + // so that isAlive() can work + eetop = Thread.NO_REF; + } } }