8304919: Implementation of Virtual Threads

Reviewed-by: lmesnik, cjplummer, psandoz, mchung, sspitsyn, jpai
This commit is contained in:
Alan Bateman 2023-04-11 05:49:54 +00:00
parent 39398075b7
commit 2586f36120
205 changed files with 1379 additions and 1342 deletions

View file

@ -46,7 +46,6 @@ import java.security.PrivilegedExceptionAction;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import jdk.internal.javac.PreviewFeature;
import jdk.internal.ref.CleanerFactory;
import sun.security.util.SecurityConstants;
@ -249,9 +248,8 @@ public class Executors {
* @param threadFactory the factory to use when creating new threads
* @return a new executor that creates a new Thread for each task
* @throws NullPointerException if threadFactory is null
* @since 19
* @since 21
*/
@PreviewFeature(feature = PreviewFeature.Feature.VIRTUAL_THREADS)
public static ExecutorService newThreadPerTaskExecutor(ThreadFactory threadFactory) {
return ThreadPerTaskExecutor.create(threadFactory);
}
@ -265,10 +263,8 @@ public class Executors {
* that creates virtual threads.
*
* @return a new executor that creates a new virtual Thread for each task
* @throws UnsupportedOperationException if preview features are not enabled
* @since 19
* @since 21
*/
@PreviewFeature(feature = PreviewFeature.Feature.VIRTUAL_THREADS)
public static ExecutorService newVirtualThreadPerTaskExecutor() {
ThreadFactory factory = Thread.ofVirtual().factory();
return newThreadPerTaskExecutor(factory);

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 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
@ -325,15 +325,14 @@ class ThreadPerTaskExecutor extends ThreadContainer implements ExecutorService {
* notified when the task completes.
*/
private static class ThreadBoundFuture<T>
extends CompletableFuture<T> implements Runnable {
extends FutureTask<T> implements Runnable {
final ThreadPerTaskExecutor executor;
final Callable<T> task;
final Thread thread;
ThreadBoundFuture(ThreadPerTaskExecutor executor, Callable<T> task) {
super(task);
this.executor = executor;
this.task = task;
this.thread = executor.newThread(this);
}
@ -342,28 +341,8 @@ class ThreadPerTaskExecutor extends ThreadContainer implements ExecutorService {
}
@Override
public void run() {
if (Thread.currentThread() != thread) {
// should not happen except where something casts this object
// to a Runnable and invokes the run method.
throw new WrongThreadException();
}
try {
T result = task.call();
complete(result);
} catch (Throwable e) {
completeExceptionally(e);
} finally {
executor.taskComplete(thread);
}
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
boolean cancelled = super.cancel(mayInterruptIfRunning);
if (cancelled && mayInterruptIfRunning)
thread.interrupt();
return cancelled;
protected void done() {
executor.taskComplete(thread);
}
}