Skip to content
Permalink
Browse files
Restore latest thread executor after termination
  • Loading branch information
AlanBateman committed Mar 25, 2021
1 parent 25aaf9e commit a6609184379c71e72da2ece71f74eb3f52e5a70b
Showing 1 changed file with 26 additions and 13 deletions.
@@ -224,19 +224,10 @@ public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedE
}
}

@Override
public void close() {
checkPermission();
checkOwner();

if (owner != null && !closed) {
if (ThreadFields.latestThreadExecutor(owner) != this)
throw new IllegalStateException("close is out of order");
// eagerly mark as closed and restore ref to previous executor
closed = true;
ThreadFields.setLatestThreadExecutor(previous);
}

/**
* Waits for executor to terminate.
*/
private void awaitTermination() {
boolean terminated = isTerminated();
if (!terminated) {
tryShutdownAndTerminate(false);
@@ -257,6 +248,28 @@ public void close() {
}
}

@Override
public void close() {
checkPermission();

if (owner != null) {
if (owner != Thread.currentThread())
throw new IllegalCallerException("Not owned by this thread");
if (!closed && ThreadFields.latestThreadExecutor(owner) != this)
throw new IllegalStateException("close is out of order");
}

try {
awaitTermination();
} finally {
if (owner != null && !closed) {
// restore ref to previous executor
ThreadFields.setLatestThreadExecutor(previous);
closed = true;
}
}
}

/**
* Invoked when the timeout expires.
*/

0 comments on commit a660918

Please sign in to comment.