你好,我是猿java。

今天分享的内容是 Java 的一个重量级功用:虚拟线程。

布景

2022-09-20,JDK 19 发布了GA版别,备受瞩目的协程功用也算尘土落地,不过,此次 GA版别并不是以协程来命名,而是运用了 Virtual
Thread(虚拟线程),并且是 preview预览版别。小编最早关注到协程功用是在 2020年,那时孵化项目叫做 Java project Loom,
运用的是 Fiber(直译为:纤维,意译为:轻量级线程,即协程),可是 GA版别为何终究被界说为 Virtual Thread(虚拟线程),原因不得而知。

GA: General Availability,正式发布的版别,在国外都是用 GA来指代 release版别;

JEP: JDK Enhancement Proposal, JDK增强主张,JEP是一个JDK中心技术相关的增强主张文档;

为什么需求虚拟线程

已然 Java官方推出一个和线程这么附近的概念,必定是要处理线程的某些问题,因而,咱们先回忆下线程的一些特点:

  • Java中的线程是对操作体系线程的一个简略包装,线程的创立,调度和毁掉等都是由操作体系完结;
  • 线程切换需求耗费CPU时刻,这部分时刻是与业务无关的;
  • 线程的功用直承受操作体系处理才能的影响;

因而,线程是一种重量级的资源,作为一名 Java程序员应该深有体会。所以,为了更好的办理线程,Java选用了池化(线程池)的方法进行办理线程,防止线程频频创立和毁掉带来的开支。可是,虽然线程池防止线程大部分创立和毁掉的开支,可是线程的调度仍是直承受操作体系的影响,那么有没有更好的方法来打破这种约束,因而,虚拟线程就孕育而生。

在 JDK 19源码中,官方直接在 java.lang包下新增一个 VirtualThread类来表示虚拟线程,为了更好的区别虚拟线程和原有的 Thread线程,官方给 Thread类赋予了一个巨大上的姓名:渠道线程。

下面给出了 JDK 19中虚拟线程的 Diagram截图以及渠道线程和体系线程的联系图:

千呼万唤始出来:Java终于发布了

千呼万唤始出来:Java终于发布了

想了解更多联系线程的常识,能够参阅往期的文章:深度剖析:Java线程运转机制,程序员必看的常识点!

怎么创立虚拟线程

1.经过 Thread.startVirtualThread()创立

如下示例代码,经过 Thread.startVirtualThread()能够创立一个新的并且已发动的虚拟线程,该方法等价于 Thread.ofVirtual().start(task):

public class VirtualThreadTest {
public static void main(String[] args) {  
CustomThread customThread = new CustomThread();  
// 创立并且发动虚拟线程  
Thread.startVirtualThread(customThread);  
}  
}  
class CustomThread implements Runnable {  
@Override  
public void run() {  
System.out.println("CustomThread run");  
}  
}  

2.经过 Thread.ofVirtual()创立

如下示例代码,经过 Thread.ofVirtual().unstarted()方法能够创立一个新的未发动的虚拟线程,然后经过 Thread.start()来发动线程,也能够经过 Thread.ofVirtual().start()直接创立一个新的并已发动的虚拟线程:

public class VirtualThreadTest {
public static void main(String[] args) {  
CustomThread customThread = new CustomThread();  
// 创立并且不发动虚拟线程,然后 unStarted.start()方法发动虚拟线程  
Thread unStarted = Thread.ofVirtual().unstarted(customThread);  
unStarted.start();  
// 等同于  
Thread.ofVirtual().start(customThread);  
}  
}  
class CustomThread implements Runnable {  
@Override  
public void run() {  
System.out.println("CustomThread run");  
}  
}  

3.经过 ThreadFactory创立

如下示例代码,经过 ThreadFactory.newThread()方法就能创立一个虚拟线程,然后经过 Thread.start()来发动线程:

public class VirtualThreadTest {
public static void main(String[] args) {  
CustomThread customThread = new CustomThread();  
// 获取线程工厂类  
ThreadFactory factory = Thread.ofVirtual().factory();  
// 创立虚拟线程  
Thread thread = factory.newThread(customThread);  
// 发动线程  
thread.start();  
}  
}  
class CustomThread implements Runnable {  
@Override  
public void run() {  
System.out.println("CustomThread run");  
}  
}  

4.经过 Executors.newVirtualThreadPerTaskExecutor()创立

如下示例代码,经过 JDK自带的Executors东西类方法创立一个虚拟线程,然后经过 executor.submit()来发动线程:

public class VirtualThreadTest {
public static void main(String[] args) {  
CustomThread customThread = new CustomThread();  
ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();  
executor.submit(customThread);  
}  
}  
class CustomThread implements Runnable {  
@Override  
public void run() {  
System.out.println("CustomThread run");  
}  
}  

经过上述列举的 4种创立虚拟线程的方法能够看出,官方为了降低虚拟线程的门槛,极力复用原有的Thread线程类,这样能够滑润的过渡到虚拟线程的运用。不过,在
Java 19中,虚拟线程仍是一个预览功用,默认封闭,需求运用参数 –enable-preview 来启用该功用,预览功用源码和发动虚拟线程指令如下:

// Thread 源码,经过 @PreviewFeature 注解来标示 虚拟线程为 预览功用  
public class Thread implements Runnable {  
/**  
* Creates a virtual thread to execute a task and schedules it to execute.  
This method is equivalent to: Thread.ofVirtual().start(task);  
Params: task – the object to run when the thread executes  
Returns: a new, and started, virtual thread  
Throws: UnsupportedOperationException – if preview features are not enabled  
Since: 19  
See Also: Inheritance when creating threads  
* @param task  
* @return  
*/  
@PreviewFeature(feature = PreviewFeature.Feature.VIRTUAL_THREADS)  
public static Thread startVirtualThread(Runnable task) {  
Objects.requireNonNull(task);  
// 判别是否敞开虚拟线程功用  
PreviewFeatures.ensureEnabled();  
var thread = ThreadBuilders.newVirtualThread(null, null, 0, task);  
thread.start();  
return thread;  
}  
// 异常信息提示 能够经过 --enable-preview 敞开虚拟线程功用  
public static void ensureEnabled() {  
if (!isEnabled()) {  
throw new UnsupportedOperationException(  
"Preview Features not enabled, need to run with --enable-preview");  
}  
}  
}  
# 敞开虚拟线程功用
java --source 19 --enable-preview XXX.java  

IDEA 中配置 –enable-preview 如下图:

千呼万唤始出来:Java终于发布了

为了更好的感受虚拟线程的功用,咱们模仿一个对比测验用例:别离运用虚拟线程和线程池履行10w个使命,每个线程使命睡觉10ms,统计各自的总耗时和创立的最大渠道线程总数,示例代码如下:

// 虚拟线程  
public class VirtualThreadTest {  
static List<Integer> list = new ArrayList<>();  
public static void main(String[] args) {  
// 敞开一个线程来监控当时的渠道线程(体系线程)总数  
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);  
scheduledExecutorService.scheduleAtFixedRate(() -> {  
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();  
ThreadInfo[] threadInfo = threadBean.dumpAllThreads(false, false);  
saveMaxThreadNum(threadInfo.length);  
}, 10, 10, TimeUnit.MILLISECONDS);  
long start = System.currentTimeMillis();  
ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();  
for (int i = 0; i < 10000; i++) {  
executor.submit(() -> {  
// 线程睡觉 10ms,能够等同于模仿业务耗时10ms  
try {  
TimeUnit.MILLISECONDS.sleep(10);  
} catch (InterruptedException e) {  
}  
});  
}  
executor.close();  
System.out.println("max:" + list.get(0) + " platform thread/os thread");  
System.out.printf("totalMillis:%dms\n", System.currentTimeMillis() - start);  
}  
}  
public class ThreadTest {  
static List<Integer> list = new ArrayList<>();  
public static void main(String[] args) {  
// 敞开一个线程来监控当时的渠道线程(体系线程)总数  
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);  
scheduledExecutorService.scheduleAtFixedRate(() -> {  
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();  
ThreadInfo[] threadInfo = threadBean.dumpAllThreads(false, false);  
saveMaxThreadNum(threadInfo.length);  
}, 1, 1, TimeUnit.SECONDS);  
long start = System.currentTimeMillis();  
ExecutorService executor = Executors.newFixedThreadPool(200);  
for (int i = 0; i < 100000; i++) {  
executor.submit(() -> {  
try {  
// 线程睡觉 10ms,能够等同于模仿业务耗时10ms  
TimeUnit.MILLISECONDS.sleep(10);  
} catch (InterruptedException e) {  
}  
});  
}  
executor.close();  
System.out.println("max:" + list.get(0) + " platform thread/os thread");  
System.out.printf("totalMillis:%dms\n", System.currentTimeMillis() - start);  
}  
}  
// 保存渠道线程的创立的最大总数  
public static List<Integer> saveMaxThreadNum(int num) {  
if (list.isEmpty()) {  
list.add(num);  
} else {  
Integer integer = list.get(0);  
if (num > integer) {  
list.add(0, num);  
}  
}  
return list;  
}  

两个示例的运转结果:

千呼万唤始出来:Java终于发布了

经过运转结果能够发现:

  • 运用虚拟线程履行 10w个使命总耗时为:129ms,最大创立了 18个渠道线程;

  • 运用线程池履行 10w个使命总耗时为:6103 ms,最大创立了 207个渠道线程;

  • 两者总耗时差50倍,最大创立的渠道线程总数差 10倍,因而功用差可想而知;

中心源码解析

首先从 VirtualThread类开始,源码如下:

/**
* A thread that is scheduled by the Java virtual machine rather than the operating system.  
*/  
final class VirtualThread extends BaseVirtualThread {  
/**  
* Creates a new {@code VirtualThread} to run the given task with the given  
* scheduler. If the given scheduler is {@code null} and the current thread  
* is a platform thread then the newly created virtual thread will use the  
* default scheduler. If given scheduler is {@code null} and the current  
* thread is a virtual thread then the current thread's scheduler is used.  
*  
* @param scheduler the scheduler or null  
* @param name thread name  
* @param characteristics characteristics  
* @param task the task to execute  
*/  
VirtualThread(Executor scheduler, String name, int characteristics, Runnable task) {  
super(name, characteristics, /*bound*/ false);  
Objects.requireNonNull(task);  
// choose scheduler if not specified  
if (scheduler == null) {  
Thread parent = Thread.currentThread();  
if (parent instanceof VirtualThread vparent) {  
scheduler = vparent.scheduler;  
} else {  
scheduler = DEFAULT_SCHEDULER;  
}  
}  
this.scheduler = scheduler;  
this.cont = new VThreadContinuation(this, task);  
this.runContinuation = this::runContinuation;  
}  
/**  
* 创立默认的调度器  
* Creates the default scheduler.  
*/  
@SuppressWarnings("removal")  
private static ForkJoinPool createDefaultScheduler() {  
ForkJoinWorkerThreadFactory factory = pool -> {  
PrivilegedAction<ForkJoinWorkerThread> pa = () -> new CarrierThread(pool);  
return AccessController.doPrivileged(pa);  
};  
PrivilegedAction<ForkJoinPool> pa = () -> {  
int parallelism, maxPoolSize, minRunnable;  
String parallelismValue = System.getProperty("jdk.virtualThreadScheduler.parallelism");  
String maxPoolSizeValue = System.getProperty("jdk.virtualThreadScheduler.maxPoolSize");  
String minRunnableValue = System.getProperty("jdk.virtualThreadScheduler.minRunnable");  
if (parallelismValue != null) {  
parallelism = Integer.parseInt(parallelismValue);  
} else {  
parallelism = Runtime.getRuntime().availableProcessors();  
}  
if (maxPoolSizeValue != null) {  
maxPoolSize = Integer.parseInt(maxPoolSizeValue);  
parallelism = Integer.min(parallelism, maxPoolSize);  
} else {  
maxPoolSize = Integer.max(parallelism, 256);  
}  
if (minRunnableValue != null) {  
minRunnable = Integer.parseInt(minRunnableValue);  
} else {  
minRunnable = Integer.max(parallelism / 2, 1);  
}  
Thread.UncaughtExceptionHandler handler = (t, e) -> { };  
boolean asyncMode = true; // FIFO  
return new ForkJoinPool(parallelism, factory, handler, asyncMode,  
0, maxPoolSize, minRunnable, pool -> true, 30, SECONDS);  
};  
return AccessController.doPrivileged(pa);  
}  
}  

经过 VirtualThread类的源码能够总结出:

  • VirtualThread承继 BaseVirtualThread类,BaseVirtualThread类承继 Thread类;
  • 虚拟线程是 JVM进行调度的,而不是操作体系;
  • VirtualThread类是一个终态类,因而该类无法被承继,无法被扩展;

VirtualThread类,只提供了一个结构器,接纳 4个参数:

  • Executor scheduler:假如给定的调度器为空并且当时线程是渠道线程,那么新创立的虚拟线程将运用默认调度程序(底层选用 ForkJoinPool),假如给定的调度器为空并且当时线程是虚拟线程,则运用当时线程的调度程序
  • String name:自界说线程名
  • int characteristics:线程特征值
  • Runnable task:需求履行的使命

然后咱们看下 JDK中创立虚拟线程的源码:

public class Thread implements Runnable {
/**  
* Creates a virtual thread to execute a task and schedules it to execute.  
This method is equivalent to: Thread.ofVirtual().start(task);  
Params: task – the object to run when the thread executes  
Returns: a new, and started, virtual thread  
Throws: UnsupportedOperationException – if preview features are not enabled  
Since: 19  
See Also: Inheritance when creating threads  
* @param task  
* @return  
*/  
@PreviewFeature(feature = PreviewFeature.Feature.VIRTUAL_THREADS)  
public static Thread startVirtualThread(Runnable task) {  
Objects.requireNonNull(task);  
// 判别是否敞开虚拟线程功用  
PreviewFeatures.ensureEnabled();  
var thread = ThreadBuilders.newVirtualThread(null, null, 0, task);  
thread.start();  
return thread;  
}  
// 异常信息提示 能够经过 --enable-preview 敞开虚拟线程功用  
public static void ensureEnabled() {  
if (!isEnabled()) {  
throw new UnsupportedOperationException(  
"Preview Features not enabled, need to run with --enable-preview");  
}  
}  
}  
class ThreadBuilders {  
static Thread newVirtualThread(Executor scheduler,  
String name,  
int characteristics,  
Runnable task) {  
if (ContinuationSupport.isSupported()) {  
return new VirtualThread(scheduler, name, characteristics, task);  
} else {  
if (scheduler != null)  
throw new UnsupportedOperationException();  
return new BoundVirtualThread(name, characteristics, task);  
}  
}  
/**  
* Returns a builder for creating a virtual {@code Thread} or {@code ThreadFactory}  
* that creates virtual threads.  
*  
* @apiNote The following are examples using the builder:  
* {@snippet :  
* // Start a virtual thread to run a task.  
* Thread thread = Thread.ofVirtual().start(runnable);  
*  
* // A ThreadFactory that creates virtual threads  
* ThreadFactory factory = Thread.ofVirtual().factory();  
* }  
*  
* @return A builder for creating {@code Thread} or {@code ThreadFactory} objects.  
* @throws UnsupportedOperationException if preview features are not enabled  
* @since 19  
*/  
@PreviewFeature(feature = PreviewFeature.Feature.VIRTUAL_THREADS)  
public static Builder.OfVirtual ofVirtual() {  
PreviewFeatures.ensureEnabled();  
return new ThreadBuilders.VirtualThreadBuilder();  
}  
}  

Thread.startVirtualThread()创立虚拟线程,会调用ThreadBuilders.newVirtualThread(),终究调用 new VirtualThread()结构器来创立虚拟线程。

从上文咱们在介绍虚拟线程创立的 4种方法也能够看出,虚拟线程创立的入口在 Thread 或许 Executors 类中,和曾经运用线程或许线程池的习气保持一致。

final class VirtualThread extends BaseVirtualThread {
/**  
* Mounts this virtual thread onto the current platform thread. On  
* return, the current thread is the virtual thread.  
*/  
@ChangesCurrentThread  
private void mount() {  
// sets the carrier thread  
Thread carrier = Thread.currentCarrierThread();  
setCarrierThread(carrier);  
// sync up carrier thread interrupt status if needed  
if (interrupted) {  
carrier.setInterrupt();  
} else if (carrier.isInterrupted()) {  
synchronized (interruptLock) {  
// need to recheck interrupt status  
if (!interrupted) {  
carrier.clearInterrupt();  
}  
}  
}  
// set Thread.currentThread() to return this virtual thread  
carrier.setCurrentThread(this);  
}  
/**  
* Unmounts this virtual thread from the carrier. On return, the  
* current thread is the current platform thread.  
*/  
@ChangesCurrentThread  
private void unmount() {  
// set Thread.currentThread() to return the platform thread  
Thread carrier = this.carrierThread;  
carrier.setCurrentThread(carrier);  
// break connection to carrier thread, synchronized with interrupt  
synchronized (interruptLock) {  
setCarrierThread(null);  
}  
carrier.clearInterrupt();  
}  
}  

mount() 和 unmount() 是虚拟线程两个中心方法:

  • mount(),能够将此虚拟线程挂载到当时渠道线程上,回来时,当时线程是虚拟线程;
  • unmount(),从载体线程卸载此虚拟线程,回来时,当时线程是渠道线程

经过这两个方法能够看出虚拟线程是搭载在渠道线程上运转,运转完毕后,从渠道线程上卸载。

虚拟线程的状况和转换

下表总结了虚拟线程中的一切线程状况以及状况之间转化的条件:

状况 转换条件
NEW -> STARTED Thread.start
STARTED -> TERMINATED failed to start
STARTED -> RUNNING first run
RUNNING -> PARKING Thread attempts to park
PARKING -> PARKED cont.yield successful, thread is parked
PARKING -> PINNED cont.yield failed, thread is pinned
PARKED -> RUNNABLE unpark or interrupted
PINNED -> RUNNABLE unpark or interrupted
RUNNABLE -> RUNNING continue execution
RUNNING -> YIELDING Thread.yield
YIELDING -> RUNNABLE yield successful
YIELDING -> RUNNING yield failed
RUNNING -> TERMINATED done

3种线程的联系

VirtualThread,Platform Thread,OS Thread 三者的联系如下图:

千呼万唤始出来:Java终于发布了

阐明:
在现有的线程模型下,一个 Java线程相当于一个操作体系线程,多个虚拟线程需求挂载在一个渠道线程(载体线程)上,每个渠道线程和体系线程一一对应。因而,VirtualThread是属于 JVM等级的线程,由JVM调度,它是十分轻量级的资源,运用完后立即被毁掉,因而就不需求像渠道线程相同运用池化(线程池)。
虚拟线程在履行到 IO 操作或 Blocking操作时,会主动切换到其他虚拟线程履行,从而防止当时线程等待,能够高效经过少量线程去调度很多虚拟线程,最大化提高线程的履行功率。

总结

  • Virtual Thread将会在功用上带来的巨大进步,不过,现在业界80~90%的代码还跑在 Java 8上,等 JDK
    19投入实际出产环境,可能需求一个漫长的过程;
  • 虚拟线程高度复用了现有的 Thread线程的功用,方便现有方法滑润迁移到虚拟线程;
  • 虚拟线程是将 Thread作为载体线程,它并没有改变原来的线程模型;
  • 虚拟线程是 JVM调度的,而不是操作体系调度;
  • 运用虚拟线程能够显著进步程序吞吐量;
  • 虚拟线程合适 并发使命数量很高 或许 IO密集型的场景,关于 核算密集型使命还需经过过增加CPU中心处理,或许利用分布式核算资源来来处理;
  • 虚拟线程现在仅仅一个预览功用,只能从源码和简略的测验来分析,并无实在出产环境的验证;

曾一段时刻内,JDK一直致力于 Reactor呼应式编程,企图从这条路子来提高 Java的功用,可是终究发现:呼应式编程难了解,难调试,难运用,
因而又把焦点转向了同步编程,为了改善功用,虚拟线程诞生了。或许虚拟线程很难在短时刻内运用到实际出产中,可是经过官方的JDK版别发布,咱们能够看到:虽然是 Oracle这样的科技型巨子也会走弯路,了解 JDK的动态,能够协助咱们更好的把握学习 Java的重心以及后面的发展趋势。

参阅

Virtual Thread JEP

java-virtual-threads