undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
191
test/jdk/java/util/concurrent/Executors/AutoShutdown.java
Normal file
191
test/jdk/java/util/concurrent/Executors/AutoShutdown.java
Normal file
|
|
@ -0,0 +1,191 @@
|
|||
/*
|
||||
* Copyright (c) 2006, 2025, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @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;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class AutoShutdown {
|
||||
|
||||
private static Stream<Supplier<ExecutorService>> executors() {
|
||||
return Stream.of(
|
||||
() -> Executors.newSingleThreadExecutor(),
|
||||
() -> Executors.newSingleThreadExecutor(Executors.defaultThreadFactory())
|
||||
);
|
||||
}
|
||||
|
||||
private static Stream<Arguments> executorAndQueuedTaskCounts() {
|
||||
int[] queuedTaskCounts = { 0, 1, 2 };
|
||||
return executors().flatMap(s -> IntStream.of(queuedTaskCounts)
|
||||
.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.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("executors")
|
||||
void testNoWorker(Supplier<ExecutorService> supplier) throws Exception {
|
||||
ExecutorService executor = supplier.get();
|
||||
ExecutorService delegate = getDelegate(executor);
|
||||
executor = null;
|
||||
gcAndAwaitTermination(delegate);
|
||||
}
|
||||
|
||||
/**
|
||||
* SingleThreadExecutor with an idle worker thread.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("executors")
|
||||
void testIdleWorker(Supplier<ExecutorService> supplier) throws Exception {
|
||||
ExecutorService executor = supplier.get();
|
||||
// submit a task to get a worker to start
|
||||
executor.submit(() -> null).get();
|
||||
ExecutorService delegate = getDelegate(executor);
|
||||
executor = null;
|
||||
gcAndAwaitTermination(delegate);
|
||||
}
|
||||
|
||||
/**
|
||||
* SingleThreadExecutor with an active worker and queued tasks.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("executorAndQueuedTaskCounts")
|
||||
void testActiveWorker(Supplier<ExecutorService> supplier,int queuedTaskCount) throws Exception {
|
||||
ExecutorService executor = supplier.get();
|
||||
// the worker will execute one task, the other tasks will be queued
|
||||
int ntasks = 1 + queuedTaskCount;
|
||||
AtomicInteger completedTaskCount = new AtomicInteger();
|
||||
for (int i = 0; i < ntasks; i++) {
|
||||
executor.submit(() -> {
|
||||
Thread.sleep(Duration.ofMillis(500));
|
||||
completedTaskCount.incrementAndGet();
|
||||
return null;
|
||||
});
|
||||
}
|
||||
ExecutorService delegate = getDelegate(executor);
|
||||
executor = null;
|
||||
gcAndAwaitTermination(delegate);
|
||||
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);
|
||||
try {
|
||||
Class<?> isolatedClass = classLoader.loadClass("AutoShutdown$IsolatedClass");
|
||||
assertSame(isolatedClass.getClassLoader(), classLoader);
|
||||
isolatedClass.getDeclaredMethod("shutdown", Consumer.class).invoke(null, shutdown);
|
||||
|
||||
isolatedClass = null;
|
||||
classLoader = null;
|
||||
|
||||
assertTrue(ForceGC.wait(() -> queue.poll() != null));
|
||||
} finally {
|
||||
Reference.reachabilityFence(reference);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the delegate for the given ExecutorService. The given ExecutorService
|
||||
* must be a Executors$DelegatedExecutorService.
|
||||
*/
|
||||
private ExecutorService getDelegate(ExecutorService executor) throws Exception {
|
||||
Field eField = Class.forName("java.util.concurrent.Executors$DelegatedExecutorService")
|
||||
.getDeclaredField("e");
|
||||
eField.setAccessible(true);
|
||||
return (ExecutorService) eField.get(executor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes System.gc and waits for the given ExecutorService to terminate.
|
||||
*/
|
||||
private void gcAndAwaitTermination(ExecutorService executor) throws Exception {
|
||||
System.err.println(executor);
|
||||
boolean terminated = false;
|
||||
while (!terminated) {
|
||||
System.gc();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
117
test/jdk/java/util/concurrent/Executors/PrivilegedCallables.java
Normal file
117
test/jdk/java/util/concurrent/Executors/PrivilegedCallables.java
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
/*
|
||||
* Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 6552961 6558429
|
||||
* @summary Test privilegedCallable, privilegedCallableUsingCurrentClassLoader
|
||||
* @run main PrivilegedCallables
|
||||
* @author Martin Buchholz
|
||||
*/
|
||||
|
||||
import static java.util.concurrent.Executors.privilegedCallable;
|
||||
import static java.util.concurrent.Executors.privilegedCallableUsingCurrentClassLoader;
|
||||
import static java.util.concurrent.Executors.privilegedThreadFactory;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class PrivilegedCallables {
|
||||
Callable<Integer> real;
|
||||
|
||||
final Callable<Integer> realCaller = new Callable<>() {
|
||||
public Integer call() throws Exception {
|
||||
return real.call(); }};
|
||||
|
||||
final Random rnd = new Random();
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
final Throwable[] throwables = {
|
||||
new Exception() {},
|
||||
new RuntimeException() {},
|
||||
new Error() {}
|
||||
};
|
||||
Throwable randomThrowable() {
|
||||
return throwables[rnd.nextInt(throwables.length)];
|
||||
}
|
||||
void throwThrowable(Throwable t) throws Exception {
|
||||
if (t instanceof Error) throw (Error) t;
|
||||
if (t instanceof RuntimeException) throw (RuntimeException) t;
|
||||
throw (Exception) t;
|
||||
}
|
||||
|
||||
void test(String[] args) {
|
||||
try { test(privilegedCallable(realCaller)); }
|
||||
catch (Throwable t) { unexpected(t); }
|
||||
|
||||
try { test(privilegedCallableUsingCurrentClassLoader(realCaller)); }
|
||||
catch (Throwable t) { unexpected(t); }
|
||||
|
||||
try { privilegedThreadFactory(); }
|
||||
catch (Throwable t) { unexpected(t); }
|
||||
}
|
||||
|
||||
void test(final Callable<Integer> c) throws Throwable {
|
||||
for (int i = 0; i < 20; i++)
|
||||
if (rnd.nextBoolean()) {
|
||||
final Throwable t = randomThrowable();
|
||||
real = new Callable<>() {
|
||||
public Integer call() throws Exception {
|
||||
throwThrowable(t);
|
||||
return null; }};
|
||||
try {
|
||||
c.call();
|
||||
fail("Expected exception not thrown");
|
||||
} catch (Throwable tt) { check(t == tt); }
|
||||
} else {
|
||||
final int n = rnd.nextInt();
|
||||
real = new Callable<>() {
|
||||
public Integer call() { return n; }};
|
||||
equal(c.call(), n);
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------- Infrastructure ---------------------------
|
||||
volatile int passed = 0, failed = 0;
|
||||
void pass() {passed++;}
|
||||
void fail() {failed++; Thread.dumpStack();}
|
||||
void fail(String msg) {System.err.println(msg); fail();}
|
||||
void unexpected(Throwable t) {failed++; t.printStackTrace();}
|
||||
void check(boolean cond) {if (cond) pass(); else fail();}
|
||||
void equal(Object x, Object y) {
|
||||
if (x == null ? y == null : x.equals(y)) pass();
|
||||
else fail(x + " not equal to " + y);}
|
||||
public static void main(String[] args) throws Throwable {
|
||||
new PrivilegedCallables().instanceMain(args);}
|
||||
void instanceMain(String[] args) throws Throwable {
|
||||
try {test(args);} catch (Throwable t) {unexpected(t);}
|
||||
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
||||
if (failed > 0) throw new AssertionError("Some tests failed");}
|
||||
abstract class F {abstract void f() throws Throwable;}
|
||||
void THROWS(Class<? extends Throwable> k, F... fs) {
|
||||
for (F f : fs)
|
||||
try {f.f(); fail("Expected " + k.getName() + " not thrown");}
|
||||
catch (Throwable t) {
|
||||
if (k.isAssignableFrom(t.getClass())) pass();
|
||||
else unexpected(t);}}
|
||||
}
|
||||
142
test/jdk/java/util/concurrent/Executors/Throws.java
Normal file
142
test/jdk/java/util/concurrent/Executors/Throws.java
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
/*
|
||||
* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 6398290
|
||||
* @summary Check Executors/STPE Exception specifications
|
||||
* @author Martin Buchholz
|
||||
*/
|
||||
|
||||
import static java.util.concurrent.Executors.callable;
|
||||
import static java.util.concurrent.Executors.defaultThreadFactory;
|
||||
import static java.util.concurrent.Executors.newCachedThreadPool;
|
||||
import static java.util.concurrent.Executors.newFixedThreadPool;
|
||||
import static java.util.concurrent.Executors.newScheduledThreadPool;
|
||||
import static java.util.concurrent.Executors.newSingleThreadExecutor;
|
||||
import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
|
||||
import static java.util.concurrent.Executors.privilegedCallable;
|
||||
import static java.util.concurrent.Executors.unconfigurableExecutorService;
|
||||
import static java.util.concurrent.Executors.unconfigurableScheduledExecutorService;
|
||||
|
||||
import java.security.PrivilegedAction;
|
||||
import java.security.PrivilegedExceptionAction;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.RejectedExecutionHandler;
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
|
||||
public class Throws {
|
||||
private static void realMain(String[] args) throws Throwable {
|
||||
final ThreadFactory fac = defaultThreadFactory();
|
||||
final ThreadFactory nullFactory = null;
|
||||
final RejectedExecutionHandler reh
|
||||
= new RejectedExecutionHandler() {
|
||||
public void rejectedExecution(Runnable r,
|
||||
ThreadPoolExecutor executor) {}};
|
||||
final RejectedExecutionHandler nullHandler = null;
|
||||
|
||||
THROWS(NullPointerException.class,
|
||||
() -> newFixedThreadPool(3, null),
|
||||
() -> newCachedThreadPool(null),
|
||||
() -> newSingleThreadScheduledExecutor(null),
|
||||
() -> newScheduledThreadPool(0, null),
|
||||
() -> unconfigurableExecutorService(null),
|
||||
() -> unconfigurableScheduledExecutorService(null),
|
||||
() -> callable(null, "foo"),
|
||||
() -> callable((Runnable) null),
|
||||
() -> callable((PrivilegedAction<?>) null),
|
||||
() -> callable((PrivilegedExceptionAction<?>) null),
|
||||
() -> privilegedCallable((Callable<?>) null),
|
||||
() -> new ScheduledThreadPoolExecutor(0, nullFactory),
|
||||
() -> new ScheduledThreadPoolExecutor(0, nullFactory, reh),
|
||||
() -> new ScheduledThreadPoolExecutor(0, fac, nullHandler));
|
||||
|
||||
THROWS(IllegalArgumentException.class,
|
||||
() -> newFixedThreadPool(-42),
|
||||
() -> newFixedThreadPool(0),
|
||||
() -> newFixedThreadPool(-42, fac),
|
||||
() -> newFixedThreadPool(0, fac),
|
||||
() -> newScheduledThreadPool(-42),
|
||||
() -> new ScheduledThreadPoolExecutor(-42),
|
||||
() -> new ScheduledThreadPoolExecutor(-42, reh),
|
||||
() -> new ScheduledThreadPoolExecutor(-42, fac, reh));
|
||||
|
||||
try { newFixedThreadPool(1).shutdownNow(); pass(); }
|
||||
catch (Throwable t) { unexpected(t); }
|
||||
|
||||
try { newFixedThreadPool(1, fac).shutdownNow(); pass(); }
|
||||
catch (Throwable t) { unexpected(t); }
|
||||
|
||||
try { newSingleThreadExecutor().shutdownNow(); pass(); }
|
||||
catch (Throwable t) { unexpected(t); }
|
||||
|
||||
try { newCachedThreadPool().shutdownNow(); pass(); }
|
||||
catch (Throwable t) { unexpected(t); }
|
||||
|
||||
try { newSingleThreadScheduledExecutor().shutdownNow(); pass(); }
|
||||
catch (Throwable t) { unexpected(t); }
|
||||
|
||||
try { newSingleThreadScheduledExecutor(fac).shutdownNow(); pass(); }
|
||||
catch (Throwable t) { unexpected(t); }
|
||||
|
||||
try { newScheduledThreadPool(0).shutdownNow(); pass(); }
|
||||
catch (Throwable t) { unexpected(t); }
|
||||
|
||||
try { newScheduledThreadPool(0, fac).shutdownNow(); pass(); }
|
||||
catch (Throwable t) { unexpected(t); }
|
||||
|
||||
try { new ScheduledThreadPoolExecutor(0).shutdownNow(); pass(); }
|
||||
catch (Throwable t) { unexpected(t); }
|
||||
|
||||
try { new ScheduledThreadPoolExecutor(0, fac).shutdownNow(); pass(); }
|
||||
catch (Throwable t) { unexpected(t); }
|
||||
|
||||
try { new ScheduledThreadPoolExecutor(0, fac, reh).shutdownNow(); pass(); }
|
||||
catch (Throwable t) { unexpected(t); }
|
||||
|
||||
}
|
||||
|
||||
//--------------------- Infrastructure ---------------------------
|
||||
static volatile int passed = 0, failed = 0;
|
||||
static void pass() {passed++;}
|
||||
static void fail() {failed++; Thread.dumpStack();}
|
||||
static void fail(String msg) {System.out.println(msg); fail();}
|
||||
static void unexpected(Throwable t) {failed++; t.printStackTrace();}
|
||||
static void check(boolean cond) {if (cond) pass(); else fail();}
|
||||
static void equal(Object x, Object y) {
|
||||
if (x == null ? y == null : x.equals(y)) pass();
|
||||
else fail(x + " not equal to " + y);}
|
||||
public static void main(String[] args) throws Throwable {
|
||||
try {realMain(args);} catch (Throwable t) {unexpected(t);}
|
||||
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
||||
if (failed > 0) throw new AssertionError("Some tests failed");}
|
||||
interface Fun {void f() throws Throwable;}
|
||||
static void THROWS(Class<? extends Throwable> k, Fun... fs) {
|
||||
for (Fun f : fs)
|
||||
try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
|
||||
catch (Throwable t) {
|
||||
if (k.isAssignableFrom(t.getClass())) pass();
|
||||
else unexpected(t);}}
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8308235
|
||||
* @summary Unreference ExecutorService objects returned by the Executors without shutdown
|
||||
* and termination, this should not leak memory
|
||||
* @run main/othervm -Xmx32m UnreferencedExecutor
|
||||
*/
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class UnreferencedExecutor {
|
||||
|
||||
private static final int DURATION_IN_SECONDS = 5;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
int ncores = Runtime.getRuntime().availableProcessors();
|
||||
long durationNanos = Duration.ofSeconds(DURATION_IN_SECONDS).toNanos();
|
||||
long start = System.nanoTime();
|
||||
while (System.nanoTime() - start < durationNanos) {
|
||||
Executors.newFixedThreadPool(ncores);
|
||||
Executors.newCachedThreadPool();
|
||||
Executors.newVirtualThreadPerTaskExecutor();
|
||||
Executors.newWorkStealingPool(ncores);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue