首页 > 编程语言 >java virtual thread

java virtual thread

时间:2023-04-04 16:58:42浏览次数:47  
标签:java thread virtual platform threads scheduler OS

A virtual thread is an instance of java.lang.Thread that is not tied to a particular OS thread.

A platform thread, by contrast, is an instance of java.lang.
Thread implemented in the traditional way, as a thin wrapper around an OS thread.

Application code in the thread-per-request style can run in a virtual thread for the entire duration of a request,
but the virtual thread consumes an OS thread only while it performs calculations on the CPU.
The result is the same scalability as the asynchronous style, except it is achieved transparently:
When code running in a virtual thread calls a blocking I/O operation in the java.* API,
the runtime performs a non-blocking OS call and automatically suspends the virtual thread until it can be resumed later.

To Java developers, virtual threads are simply threads that are cheap to create and almost infinitely plentiful.
Hardware utilization is close to optimal, allowing a high level of concurrency and, as a result,
high throughput, while the application remains harmonious with the multithreaded design of the Java Platform and its tooling.

light weight

Virtual threads are cheap and plentiful, and thus should never be pooled:
A new virtual thread should be created for every application task.
Most virtual threads will thus be short-lived and have shallow call stacks,
performing as little as a single HTTP client call or a single JDBC query.

Platform threads, by contrast, are heavyweight and expensive, and thus often must be pooled.
They tend to be long-lived, have deep call stacks, and be shared among many tasks.

Scheduler

The JDK's virtual thread scheduler is a work-stealing ForkJoinPool that operates in FIFO mode.
The parallelism of the scheduler is the number of platform threads available for the purpose of scheduling virtual threads.
By default it is equal to the number of available processors, but it can be tuned with the system property jdk.virtualThreadScheduler.

Execution

To take advantage of virtual threads, it is not necessary to rewrite your program.
Virtual threads do not require or expect application code to explicitly hand back control to the scheduler;
in other words, virtual threads are not cooperative.

User code must not make assumptions about how or when virtual threads are
assigned to platform threads any more than it makes assumptions about how or
when platform threads are assigned to processor cores.

To run code in a virtual thread, the JDK's virtual thread scheduler assigns the virtual thread for execution on a platform thread by mounting the virtual thread on a platform thread.
This makes the platform thread become the carrier of the virtual thread.

Later, after running some code, the virtual thread can unmount from its carrier.
At that point the platform thread is free so the scheduler can mount a different virtual thread on it, thereby making it a carrier again.

Typically, a virtual thread will unmount when it blocks on I/O or some other blocking operation in the JDK, such as BlockingQueue.take().
When the blocking operation is ready to complete (e.g., bytes have been received on a socket),
it submits the virtual thread back to the scheduler, which will mount the virtual thread on a carrier to resume execution.

The mounting and unmounting of virtual threads happens frequently and transparently, and without blocking any OS threads.

For example, the server application shown earlier included the following line of code, which contains calls to blocking operations:

response.send(future1.get() + future2.get());

These operations will cause the virtual thread to mount and unmount multiple times,
typically once for each call to get() and possibly multiple times in the course of performing I/O in send(...).

The vast majority of blocking operations in the JDK will unmount the virtual thread, freeing its carrier and the underlying OS thread to take on new work.

However, some blocking operations in the JDK do not unmount the virtual thread,
and thus block both its carrier and the underlying OS thread.

This is because of limitations either at the OS level (e.g., many filesystem operations) or at the JDK level (e.g., Object.wait()).
The implementation of these blocking operations will compensate for the capture of the OS thread by temporarily expanding the parallelism of the scheduler. (OS 不支持阻塞等待 fs 操作,开启更多线程,类似于 node.js 读取文件使用 worker thread)

Consequently, the number of platform threads in the scheduler's ForkJoinPool may temporarily exceed the number of available processors.
The maximum number of platform threads available to the scheduler can be tuned with the system property jdk.virtualThreadScheduler.maxPoolSize.

https://openjdk.org/jeps/425

问题

virtual thread level sync primitive

比如mutex, atomic 等,

以组织virtual thread task graph。

jdk 中 是否有支持?

标签:java,thread,virtual,platform,threads,scheduler,OS
From: https://www.cnblogs.com/Searchor/p/17286948.html

相关文章

  • dubbo线程池又被打爆(打满)了java.util.concurrent.RejectedExecutionException: Thread
    转载:https://blog.csdn.net/kevin_mails/article/details/121764780?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-121764780-blog-124236206.235%5Ev27%5Epc_relevant_recovery_v2&depth_1-utm_sourc......
  • wordpress粘贴图片自动上传到服务器(Java版)
    ​ 这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下)<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@     page contentType="text/html;cha......
  • java使用浏览器请求下载pdf文件
    java使用浏览器请求下载pdf文件代码/***下载pdf文件**@parampdfFileUrl文件地址*@paramfileName文件名称*/publicstaticvoiddownloadPdf(StringpdfFileUrl,StringfileName){ServletRequestAttributesrequestAttributes=(ServletRequestAttr......
  • JavaScript:数组删除指定元素
    1.shift()方法用于删除数组中的第一个元素。注:此方法会改变数组的长度letarr=[1,2,3]arr.shift()//删除1//arr为[2,3]2.pop()方法用于删除数组中最后一个元素注:此方法会改变数组的长度letarr=[1,2,3]arr.pop();//删除3//arr为[1,2]3.splice()方法用于......
  • java抽象类和接口
    abstract由abstract关键字修饰的类称为抽象类,可以将某些类共有的行为抽象出来,形成约束,提高开发效率。//抽象类publicabstractclassAction{//抽象方法,只有方法名字,没有方法的实现publicabstractvoiddoSth();}抽象类不可以通过new关键字创建实例抽象类中可以有......
  • 使用JAVA实现布隆过滤器
    什么是布隆过滤器布隆过滤器是一种内存友好的数据结构,它可以高效地判断一个元素是否存在于一个集合中,以及大幅减少磁盘/数据库等IO操作。与哈希表和树等数据结构不同,它可以实现非常高的查找速度和存储效率,适用于需要快速并且高效地处理大数据集的场景。布隆过滤器原理布隆过滤......
  • java xxljob 根据参数运行业务
    配置定时任务不启动,手动执行根据传入的参数完成既定的业务 /** *自定义增删除平台酒体数据 *参数:startDate,endDate[yyyy-MM-dd) * *@return{@link*@return:com.xxl.job.core.biz.model.ReturnT<java.lang.String>} *@author:xxx *@date2023/3/12 **......
  • 深入理解 Java 中 SPI 机制
    vivo互联网技术微信公众号 作者:姜柱SPI(ServiceProviderInterface),是JDK内置的一种服务提供发现机制,本文由浅入深地介绍了JavaSPI机制。一、简介SPI(ServiceProviderInterface),是JDK内置的一种服务提供发现机制,可以用来启用框架扩展和替换组件,主要是被框架的开发人员使用,比如j......
  • 为什么 JavaScript 中 0.1 0.2 不等于 0.3 ?
    vivo互联网技术微信公众号 作者:刘洋在js中进行数学的运算时,会出现0.1+0.2=0.300000000000000004的结果,一开始认为是浮点数的二进制存储导致的精度问题,但这似乎不能很好的解释为什么在同样的存储方式下0.3+0.4=0.7可以得到正确的结果。本文主要通过浮点数的二进制存储及运算,和......
  • Kotlin 协程真的比 Java 线程更高效吗?
    vivo互联网技术微信公众号 作者:吴越网上几乎全部介绍Kotlin的文章都会说Kotlin的协程是多么的高效,比线程性能好很多,然而事情的真相真是如此么?协程的概念本身并不新鲜,使用C++加上内嵌汇编,一个基本的协程模型50行代码之内就可以完全搞出来。早在2013年国内就有团队开源了号称支持......