Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,13 @@ public void shutdown() {
super.shutdown();
cleanable.clean(); // unregisters the cleanable
}

@Override
public List<Runnable> shutdownNow() {
List<Runnable> unexecuted = super.shutdownNow();
cleanable.clean(); // unregisters the cleanable
return unexecuted;
}
}

/**
Expand Down
48 changes: 46 additions & 2 deletions test/jdk/java/util/concurrent/Executors/AutoShutdown.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,32 @@

/*
* @test
* @bug 6399443 8302899
* @bug 6399443 8302899 8362123
* @summary Test that Executors.newSingleThreadExecutor wraps an ExecutorService that
* automatically shuts down and terminates when the wrapper is GC'ed
* @library /test/lib/
* @modules java.base/java.util.concurrent:+open
* @run junit AutoShutdown
*/

import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.reflect.Field;
import java.time.Duration;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Stream;
import java.util.stream.IntStream;

import jdk.test.lib.Utils;
import jdk.test.lib.util.ForceGC;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -61,6 +70,10 @@ private static Stream<Arguments> executorAndQueuedTaskCounts() {
.mapToObj(i -> Arguments.of(s, i)));
}

private static Stream<Arguments> shutdownMethods() {
return Stream.<Consumer<ExecutorService>>of(e -> e.shutdown(), e -> e.shutdownNow()).map(Arguments::of);
}

/**
* SingleThreadExecutor with no worker threads.
*/
Expand Down Expand Up @@ -110,6 +123,21 @@ void testActiveWorker(Supplier<ExecutorService> supplier,int queuedTaskCount) th
assertEquals(ntasks, completedTaskCount.get());
}

@ParameterizedTest
@MethodSource("shutdownMethods")
void testShutdownUnlinksCleaner(Consumer<ExecutorService> shutdown) throws Exception {
ClassLoader classLoader = Utils.getTestClassPathURLClassLoader(ClassLoader.getPlatformClassLoader());

ReferenceQueue<?> queue = new ReferenceQueue<>();
Reference<?> reference = new PhantomReference(classLoader, queue);

classLoader.loadClass("AutoShutdown$IsolatedClass").getDeclaredMethod("shutdown", Consumer.class).invoke(null, shutdown);
classLoader = null;

assertTrue(ForceGC.wait(() -> queue.poll() != null));
Reference.reachabilityFence(reference);
}

/**
* Returns the delegate for the given ExecutorService. The given ExecutorService
* must be a Executors$DelegatedExecutorService.
Expand All @@ -132,5 +160,21 @@ private void gcAndAwaitTermination(ExecutorService executor) throws Exception {
terminated = executor.awaitTermination(100, TimeUnit.MILLISECONDS);
}
}
}

public static class IsolatedClass {

private static final ExecutorService executor = Executors.newSingleThreadExecutor(new IsolatedThreadFactory());

public static void shutdown(Consumer<ExecutorService> shutdown) {
shutdown.accept(executor);
}
}

public static class IsolatedThreadFactory implements ThreadFactory {

@Override
public Thread newThread(Runnable r) {
return new Thread(r);
}
}
}