JDK-8296673 : Implementation of Virtual Threads (Second Preview)
  • Type: CSR
  • Component: core-libs
  • Priority: P4
  • Status: Finalized
  • Resolution: Unresolved
  • Fix Versions: 20
  • Submitted: 2022-11-09
  • Updated: 2022-11-21
Related Reports
CSR :  
Relates :  
Description
Summary
-------

Virtual threads were proposed as a preview feature by JEP 425 and delivered in JDK 19. JEP 436 proposes a second preview to allow time for more feedback and to get more experience with this feature.

There are no changes since the first preview. JEP 425 (and the corresponding CSR) added a number of APIs as permanent APIs and others as preview APIs. The APIs added as preview APIs are proposed to continue to preview in JDK 20.


Problem
-------

The goal of this feature is unchanged, that is the goal is to enable server applications written in the simple thread-per-request style to scale. The Motivation section of the JEP provides a lot of discussion on this topic and how some developers workaround the limitation by giving up on the thread-per-request style in favor of thread-sharing and the asynchronous style.


Solution
--------

The APIs that are proposed to continue preview are:
```
interface java.lang.Thread.Builder
interface java.lang.Thread.Builder.OfPlatform
interface java.lang.Thread.Builder.OfVirtual
method java.lang.Thread.ofPlatform()
method java.lang.Thread.ofVirtual()
method java.lang.Thread.startVirtualThread()
method java.lang.Thread.isVirtual()
method java.util.concurrent.Executors.newThreadPerTaskExecutor(ThreadFactory)
method java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor()
method com.sun.jdi.ThreadReference.isVirtual()
method com.sun.jdi.request.ThreadStartRequest.addPlatformThreadsOnlyFilter()
method com.sun.jdi.request.ThreadDeathRequest.addPlatformThreadsOnlyFilter()
enum com.sun.management.HotSpotDiagnosticMXBean.ThreadDumpFormat
method com.sun.management.HotSpotDiagnosticMXBean.dumpThreads(String, ThreadDumpFormat)
method jdk.jfr.consumer.RecordedThread.isVirtual()
JNI function IsVirtualThread
JVMTI function SuspendAllVirtualThreads
JVMTI function ResumeAllVirtualThreads
JVMTI event VirtualThreadStart
JVMTI event VirtualThreadEnd
JVMTI capability can_support_virtual_threads
JDWP command ThreadReference/IsVirtual
JDWP modifier EventRequest/Set.PlatformThreadsOnly
```

In addition, the following APIs specify behavior for virtual threads:
```
class java.lang.Thread
class java.lang.ThreadGroup
class java.lang.management.ThreadMXBean
method java.net.Socket.connect(SocketAddress)
method java.net.Socket.connect(SocketAddress, timeout)
method java.net.Socket.getInputStream()
method java.net.Socket.getOutputStream()
method java.net.ServerSocket.accept()
method java.net.DatagramSocket.receive(DatagramPacket)
class com.sun.jdi.VirtualMachine
class com.sun.jdi.ThreadReference
class com.sun.jdi.StackFrame
class com.sun.management.ThreadMXBean
JVMTI spec sections on Thread, Local Variable, Force Early Return and Timers
JDWP spec VirtualMachine, ThreadReference and StackFrame command sets
```


The following command line options continue for this feature:
```
jcmd Thread.dump_to_file [-format=json] [-overwrite] file
jdb -trackallthreads
JDWP agent sub option includevirtualthreads=y
```

The following system properties continue for this feature:
```
jdk.virtualThreadScheduler.parallelism
jdk.virtualThreadScheduler.maxPoolSize
jdk.tracePinnedThreads
```

The following file formats continue for this feature:
```
Thread dump JSON format
```

The following Java Flight Recorder events continue for this feature:
```
jdk.VirtualThreadStart
jdk.VirtualThreadEnd
jdk.VirualThreadPinned
jdk.VirtualThreadSubmitFailed
```


Specification
-------------

There are no specification changes proposed.


Comments
Moving to Provisional.
15-11-2022