Home > Java > javaTutorial > body text

Summary of Java knowledge points: JDK19 virtual thread

WBOY
Release: 2022-10-09 14:46:16
forward
2550 people have browsed it

This article brings you relevant knowledge about java, which mainly introduces the relevant content about virtual threads in jdk19. Virtual threads are processes with goroutines and Erlang language in go language. Similar implementation methods are a form of user-mode threads. Let’s take a look at them together. I hope it will be helpful to everyone.

Summary of Java knowledge points: JDK19 virtual thread

## Recommended study: "

java video tutorial"

Introduction

Virtual threads have an implementation similar to the goroutines of the Go language and the processes of the Erlang language. They are a form of user-mode threads.

In the past, thread pools were often used in Java to share platform threads to improve the utilization of computer hardware. However, in this asynchronous style, each stage of the request may be executed on a different thread. Each thread runs phases belonging to different requests in an interleaved manner, which is inconsistent with the design of the Java platform and results in:

  • The stack trace does not provide usable context

  • The debugger cannot step through the request processing logic

  • The profiler cannot associate the cost of an operation with its caller.

Virtual threads remain compatible with the design of the platform while making optimal use of hardware without affecting scalability. Virtual threads are lightweight implementations of threads provided by the JDK rather than the operating system:

  • Virtual threads are threads that are not bound to a specific operating system thread.

  • Platform threads are threads implemented in the traditional way, as a simple wrapper around operating system threads.

Abstract

Introducing virtual threads to the Java platform. Virtual threads are lightweight threads that greatly reduce the effort of writing, maintaining, and observing high-throughput concurrent applications.

Goals

  • Allows server applications written in a simple one-thread-per-request fashion to approach the most Scaling with optimal hardware utilization.

  • Allows existing code using java.lang.ThreadAPI to adopt virtual threads with minimal changes.

  • Use existing JDK tools to easily troubleshoot, debug, and analyze virtual threads.

Non-target

  • Remove legacy implementation of threads or migrate existing applications Using virtual threads is not the goal.

  • Change Java's basic concurrency model.

  • Our goal is not to provide new data-parallel structures in the Java language or Java libraries. StreamAPI remains the preferred method for processing large data sets in parallel.

Motivation

For nearly 30 years, Java developers have relied on threads as the building blocks of concurrent server applications. Each statement in each method is executed in a thread, and since Java is multi-threaded, multiple threads of execution occur simultaneously.

A thread is Java's unit of concurrency: a piece of sequential code that runs concurrently with, and largely independent of, other such units.

Each thread provides a stack to store local variables and coordinate method calls, as well as the context when an error occurs: Exceptions are thrown and caught by methods in the same thread, so developers can use the thread's stack trace to find out what happened.

Threads are also a core concept of the tool: the debugger walks through the statements in threaded methods, and the profiler visualizes the behavior of multiple threads to help understand their performance.

Two concurrency styles

thread-per-request style

  • Server applications usually handle concurrent users independently of each other request, so it makes sense for the application to handle the request by allocating a thread to the request for the entire duration of the request. This thread-on-request style is easy to understand, easy to program, easy to debug, and easy to configure because it uses the platform's concurrency units to represent the application's concurrency units.

  • The scalability of server applications is governed by Little's Law, which relates latency, concurrency, and throughput: For a given request processing continues Time (latency), the number of requests an application can handle simultaneously (concurrency) must grow proportionally to the arrival rate (throughput).

  • For example, assume an application with an average latency of 50ms achieves a throughput of 200 requests per second by processing 10 requests concurrently. In order for this application to achieve a throughput of 2000 requests per second, it would need to handle 100 requests simultaneously. If each request is handled in a thread for the duration of the request, the number of threads must grow as throughput increases in order for the application to keep up.

  • Unfortunately, the number of available threads is limited because the JDK implements threads as wrappers for operating system (OS) threads. OS threads are expensive, so we can't have too many threads, which makes the implementation unsuitable for the one-thread-per-request style.

  • If each request consumes one thread for its duration, and thus one OS thread, the number of threads will usually be exhausted before other resources such as CPU or network connections are exhausted became a limiting factor for a long time. The JDK's current threading implementation limits application throughput to levels well below what the hardware can support. This happens even in thread pools because pools help avoid the high cost of starting new threads but do not increase the total number of threads.

asynchronous style

Some developers looking to take full advantage of their hardware have abandoned thread-per-request style, instead adopting thread-sharing style.

Instead of processing a request on one thread from start to finish, the request handling code returns its thread to a pool while waiting for the I/O operation to complete so that the thread can handle other requests. This fine-grained thread sharing (where the code only reserves a thread while performing calculations, rather than while waiting for I/O) allows for a large number of concurrent operations without consuming a large number of threads.

Although it removes the throughput limit imposed by the scarcity of operating system threads, it comes at a high cost: it requires a so-called asynchronous programming style, using a set of independent I/O methods that do not Waits for the I/O operation to complete and instead signals its completion to the callback at a later time. Without dedicated threads, developers must break down the request processing logic into small stages, usually written in the form of lambda expressions, and then combine them into a sequential pipeline with an API (see, for example, CompletableFuture, or the so-called "React" "sexuality" framework). Therefore, they abandon the language's basic sequential composition operators, such as loops and try/catch blocks.

In the asynchronous style, each phase of a request may be executed on a different thread, with each thread running phases belonging to different requests in an interleaved manner. This has profound implications for understanding program behavior:

  • The stack trace provides no usable context

  • The debugger cannot step through the request handling logic

  • The analyzer cannot associate the cost of an operation with its caller.

Combining lambda expressions is manageable when using Java's streaming API to process data in short pipes, but when all request handling code in an application must be There is a problem when writing in this way. This programming style is inconsistent with the Java platform because the application's concurrency unit (the asynchronous pipe) is no longer the platform's concurrency unit.

Comparison

Summary of Java knowledge points: JDK19 virtual thread

##Use virtual threads to retain thread-per -request style

In order for the application to scale while maintaining harmony with the platform, we should strive to maintain the one-thread-per-request style by implementing threads more efficiently so that They can be richer.

The operating system cannot implement OS threads more efficiently because different languages ​​and runtimes use thread stacks in different ways. However, the way the Java runtime implements Java threads can severe the one-to-one correspondence between them and operating system threads. Just as the operating system gives the illusion of memory sufficiency by mapping a large amount of virtual address space to a limited amount of physical RAM, the Java runtime can also give the illusion of thread sufficiency by mapping a large number of virtual address spaces to a small number of operating system threads. the illusion.

  • Virtual threads are threads that are not bound to a specific operating system thread.

  • Platform threads are threads implemented in the traditional way, as a simple wrapper around operating system threads.

thread-per-request style application code can run in a virtual thread for the entire request, but the virtual thread only uses the operating system thread when performing computations on the CPU. The result is the same scalability as the asynchronous style, except that it is implemented transparently:

When code running in a virtual thread calls a blocking I/O operation in the Java.* API, the runtime executes A non-blocking operating system call and automatically suspends the virtual thread until it can be resumed later.

For Java developers, virtual threads are threads that are cheap to create and have an almost infinite number. Hardware utilization is near optimal, allowing high levels of concurrency, thereby increasing throughput, while applications remain in tune with the multi-threaded design of the Java platform and its tools.

The significance of virtual threads

Virtual threads are cheap and abundant and therefore should never be shared (even using a thread pool): should Create a new virtual thread for each application task.

As a result, most virtual threads are short-lived and have shallow call stacks, performing as little as a single HTTP client call or a single JDBC query. In contrast, platform threads are heavyweight and expensive and therefore must often be shared. They tend to be long-lived, have deep call stacks, and are shared among many tasks.

In short, virtual threads retain a reliable thread-per-request style that is consistent with the design of the Java platform while optimally utilizing the hardware. Using virtual threads does not require learning new concepts, although it may require unlearning habits developed in response to the high cost of today's threads. Not only do virtual threads help application developers—they also help framework designers provide easy-to-use APIs that are compatible with the platform's design without compromising scalability.

Description

Today, every instance of java.lang. A thread in the JDK is a platform thread. Platform threads run Java code on underlying operating system threads and capture operating system threads throughout the code's lifetime. The number of platform threads is limited to the number of operating system threads.

Virtual thread is an instance of java.lang. A thread that runs Java code on the underlying operating system thread but does not capture that operating system thread throughout the code's lifetime. This means that many virtual threads can run their Java code on the same OS thread, effectively sharing them. The platform thread monopolizes a precious operating system thread, but the virtual thread does not. The number of virtual threads can be much larger than the number of operating system threads.

Virtual threads are lightweight implementations of threads provided by the JDK rather than the operating system. They are a form of user-mode threads that have been used successfully in other multi-threaded languages ​​(for example, goroutines in Go and processes in Erlang). User-mode threads even featured so-called "green threads" in early versions of Java, before OS threads were mature and popular. However, Java's green threads all share an OS thread (M:1 scheduling) and are eventually surpassed by platform threads and implemented as wrappers for OS threads (1:1 scheduling). Virtual threads use M:N scheduling, where a large number (M) of virtual threads are scheduled to run on fewer (N) operating system threads.

Virtual threads VS platform threads

Simple example

Developers can choose to use virtual threads Or platform thread. Below is a sample program that creates a large number of virtual threads. The program first obtains an ExecutorService, which will create a new virtual thread for each submitted task. It then submits 10,000 tasks and waits for them all to complete:

try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
    IntStream.range(0, 10000).forEach(i -> {
        executor.submit(() -> {
            Thread.sleep(Duration.ofSeconds(1));
            return i;
        });
    });
}  // executor.close() is called implicitly, and waits
Copy after login

The tasks in this example are simple code (sleep for one second) that modern hardware can easily support 10,000 virtual threads running concurrently. Behind the scenes, the JDK runs code on a handful of operating system threads, perhaps as few as one.

If this program uses ExecutorService to create a new platform thread for each task, such as Executors.newCachedThreadPool (), then the situation will be very different. The ExecutorService will try to create 10,000 platform threads, thus creating 10,000 OS threads, and the program may crash, depending on the computer and operating system.

On the contrary, if the program uses an ExecutorService that obtains platform threads from the pool (such as Executors.newFixedThreadPool (200)), the situation will not be much better. The ExecutorService will create 200 platform threads, shared by all 10,000 tasks, so many tasks will run sequentially rather than concurrently, and the program will take a long time to complete. For this program, a pool of 200 platform threads can only achieve a throughput of 200 tasks per second, while virtual threads achieve a throughput of 10,000 tasks per second (after sufficient warm-up). Furthermore, if the 10000 in the example program were changed to 1000000, then the program would submit 1,000,000 tasks, create 1,000,000 virtual threads running concurrently, and (after sufficient warm-up) achieve a throughput of approximately 1,000,000 tasks/second.

If the tasks in this program perform a one-second calculation (e.g. sort a huge array) instead of just sleeping, then increasing the number of threads beyond the number of processor cores will not help, regardless of whether they Is it a virtual thread or a platform thread.

虚拟线程并不是更快的线程ーー它们运行代码的速度并不比平台线程快。它们的存在是为了提供规模(更高的吞吐量) ,而不是速度(更低的延迟) 。它们的数量可能比平台线程多得多,因此根据 Little’s Law,它们能够实现更高吞吐量所需的更高并发性。

换句话说,虚拟线程可以显著提高应用程序的吞吐量,在如下情况时:

  • 并发任务的数量很多(超过几千个)

  • 工作负载不受 CPU 限制,因为在这种情况下,比处理器核心拥有更多的线程并不能提高吞吐量

虚拟线程有助于提高典型服务器应用程序的吞吐量,因为这类应用程序由大量并发任务组成,这些任务花费了大量时间等待。

虚拟线程可以运行平台线程可以运行的任何代码。特别是,虚拟线程支持线程本地变量和线程中断,就像平台线程一样。这意味着处理请求的现有 Java 代码很容易在虚拟线程中运行。许多服务器框架将选择自动执行此操作,为每个传入请求启动一个新的虚拟线程,并在其中运行应用程序的业务逻辑。

下面是一个服务器应用程序示例,它聚合了另外两个服务的结果。假设的服务器框架(未显示)为每个请求创建一个新的虚拟线程,并在该虚拟线程中运行应用程序的句柄代码。然后,应用程序代码创建两个新的虚拟线程,通过与第一个示例相同的 ExecutorService 并发地获取资源:

void handle(Request request, Response response) {
    var url1 = ...
    var url2 = ...
 
    try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
        var future1 = executor.submit(() -> fetchURL(url1));
        var future2 = executor.submit(() -> fetchURL(url2));
        response.send(future1.get() + future2.get());
    } catch (ExecutionException | InterruptedException e) {
        response.fail(e);
    }
}
 
String fetchURL(URL url) throws IOException {
    try (var in = url.openStream()) {
        return new String(in.readAllBytes(), StandardCharsets.UTF_8);
    }
}
Copy after login

这样的服务器应用程序使用简单的阻塞代码,可以很好地扩展,因为它可以使用大量虚拟线程。

NewVirtualThreadPerTaskExector ()并不是创建虚拟线程的唯一方法。新的 java.lang.Thread.Builder。可以创建和启动虚拟线程。此外,结构化并发提供了一个更强大的 API 来创建和管理虚拟线程,特别是在类似于这个服务器示例的代码中,通过这个 API,平台及其工具可以了解线程之间的关系。

虚拟线程是一个预览 API,默认情况下是禁用的

上面的程序使用 Executors.newVirtualThreadPerTaskExector ()方法,因此要在 JDK 19上运行它们,必须启用以下预览 API:

  • 使用javac --release 19 --enable-preview Main.java编译该程序,并使用 java --enable-preview Main 运行该程序;或者:

  • 在使用源代码启动程序时,使用 java --source 19 --enable-preview Main.java 运行程序; 或者:

  • 在使用 jshell 时,使用 jshell --enable-preview 启动它。

不要共享(pool)虚拟线程

开发人员通常会将应用程序代码从传统的基于线程池的 ExecutorService 迁移到每个任务一个虚拟线程的 ExecutorService。与所有资源池一样,线程池旨在共享昂贵的资源,但虚拟线程并不昂贵,而且从不需要共享它们。

开发人员有时使用线程池来限制对有限资源的并发访问。例如,如果一个服务不能处理超过20个并发请求,那么通过提交给大小为 20 的池的任务将确保执行对该服务的所有访问。因为平台线程的高成本使得线程池无处不在,所以这个习惯用法也变得无处不在,但是开发人员不应该为了限制并发性而将虚拟线程集中起来。应该使用专门为此目的设计的构造(如信号量semaphores)来保护对有限资源的访问。这比线程池更有效、更方便,也更安全,因为不存在线程本地数据从一个任务意外泄漏到另一个任务的风险。

观测

编写清晰的代码并不是故事的全部。对于故障排除、维护和优化来说,清晰地表示正在运行的程序的状态也是必不可少的,JDK 长期以来一直提供调试、概要分析和监视线程的机制。这样的工具对虚拟线程也应该这样做ーー也许要适应它们的大量数据ーー因为它们毕竟是 java.lang.Thread 的实例。

Java 调试器可以单步执行虚拟线程、显示调用堆栈和检查堆栈帧中的变量。JDK Flight Recorder (JFR) 是 JDK 的低开销分析和监视机制,可以将来自应用程序代码的事件(比如对象分配和 I/O 操作)与正确的虚拟线程关联起来。

这些工具不能为以异步样式编写的应用程序做这些事情。在这种风格中,任务与线程无关,因此调试器不能显示或操作任务的状态,分析器也不能告诉任务等待 I/O 所花费的时间。

线程转储( thread dump) 是另一种流行的工具,用于以每个请求一个线程的样式编写的应用程序的故障排除。遗憾的是,通过 jstack 或 jcmd 获得的 JDK 传统线程转储提供了一个扁平的线程列表。这适用于数十或数百个平台线程,但不适用于数千或数百万个虚拟线程。因此,我们将不会扩展传统的线程转储以包含虚拟线程,而是在 jcmd 中引入一种新的线程转储,以显示平台线程旁边的虚拟线程,所有这些线程都以一种有意义的方式进行分组。当程序使用结构化并发时,可以显示线程之间更丰富的关系。

因为可视化和分析大量的线程可以从工具中受益,所以 jcmd 除了纯文本之外,还可以发布 JSON 格式的新线程转储:

$ jcmd <pid> Thread.dump_to_file -format=json <file>
Copy after login

新的线程转储格式列出了在网络 I/O 操作中被阻塞的虚拟线程,以及由上面所示的 new-thread-per-task ExecutorService 创建的虚拟线程。它不包括对象地址、锁、 JNI 统计信息、堆统计信息以及传统线程转储中出现的其他信息。此外,由于可能需要列出大量线程,因此生成新的线程转储并不会暂停应用程序。

下面是这样一个线程转储的示例,它取自类似于上面第二个示例的应用程序,在 JSON 查看器中呈现 :

Summary of Java knowledge points: JDK19 virtual thread

由于虚拟线程是在 JDK 中实现的,并且不绑定到任何特定的操作系统线程,因此它们对操作系统是不可见的,操作系统不知道它们的存在。操作系统级别的监视将观察到,JDK 进程使用的操作系统线程比虚拟线程少。

调度

为了完成有用的工作,需要调度一个线程,也就是分配给处理器核心执行。对于作为 OS 线程实现的平台线程,JDK 依赖于 OS 中的调度程序。相比之下,对于虚拟线程,JDK 有自己的调度程序。JDK 的调度程序不直接将虚拟线程分配给处理器,而是将虚拟线程分配给平台线程(这是前面提到的虚拟线程的 M: N 调度)。然后,操作系统像往常一样调度平台线程。

JDK 的虚拟线程调度程序是一个在 FIFO 模式下运行的工作窃取(work-stealing) 的 ForkJoinPool。调度程序的并行性是可用于调度虚拟线程的平台线程的数量。默认情况下,它等于可用处理器的数量,但是可以使用系统属性 jdk.viralThreadScheduler.allelism 对其进行调优。注意,这个 ForkJoinPool 不同于公共池,例如,公共池用于并行流的实现,公共池以 LIFO 模式运行。

  • 虚拟线程无法获得载体(即负责调度虚拟线程的平台线程)的标识。由 Thread.currentThread ()返回的值始终是虚拟线程本身。

  • 载体和虚拟线程的堆栈跟踪是分离的。在虚拟线程中抛出的异常将不包括载体的堆栈帧。线程转储不会显示虚拟线程堆栈中其载体的堆栈帧,反之亦然。

  • 虚拟线程不能使用载体的线程本地变量,反之亦然。

此外,从 Java 代码的角度来看,虚拟线程及其载体平台线程临时共享操作系统线程的事实是不存在的。相比之下,从本机代码的角度来看,虚拟线程及其载体都在同一个本机线程上运行。因此,在同一虚拟线程上多次调用的本机代码可能会在每次调用时观察到不同的 OS 线程标识符。

调度程序当前没有实现虚拟线程的时间共享。分时是对消耗了分配的 CPU 时间的线程的强制抢占。虽然在平台线程数量相对较少且 CPU 利用率为100% 的情况下,分时可以有效地减少某些任务的延迟,但是对于一百万个虚拟线程来说,分时是否有效尚不清楚。

执行

要利用虚拟线程,不必重写程序。虚拟线程不需要或期望应用程序代码显式地将控制权交还给调度程序; 换句话说,虚拟线程不是可协作的。用户代码不能假设如何或何时将虚拟线程分配给平台线程,就像它不能假设如何或何时将平台线程分配给处理器核心一样。

为了在虚拟线程中运行代码,JDK 的虚拟线程调度程序通过将虚拟线程挂载到平台线程上来分配要在平台线程上执行的虚拟线程。这使得平台线程成为虚拟线程的载体。稍后,在运行一些代码之后,虚拟线程可以从其载体卸载。此时平台线程是空闲的,因此调度程序可以在其上挂载不同的虚拟线程,从而使其再次成为载体。

通常,当虚拟线程阻塞 I/O 或 JDK 中的其他阻塞操作(如 BlockingQueue.take ())时,它将卸载。当阻塞操作准备完成时(例如,在套接字上已经接收到字节) ,它将虚拟线程提交回调度程序,调度程序将在运营商上挂载虚拟线程以恢复执行。

虚拟线程的挂载和卸载频繁且透明,并且不会阻塞任何 OS 线程。例如,前面显示的服务器应用程序包含以下代码行,其中包含对阻塞操作的调用:

response.send(future1.get() + future2.get());
Copy after login

这些操作将导致虚拟线程多次挂载和卸载,通常每个 get ()调用一次,在 send (...)中执行 I/O 过程中可能多次挂载和卸载。

JDK 中的绝大多数阻塞操作将卸载虚拟线程,从而释放其载体和底层操作系统线程,使其承担新的工作。但是,JDK 中的一些阻塞操作不会卸载虚拟线程,因此阻塞了其载体和底层 OS 线程。这是由于操作系统级别(例如,许多文件系统操作)或 JDK 级别(例如,Object.wait ())的限制造成的。这些阻塞操作的实现将通过暂时扩展调度程序的并行性来补偿对 OS 线程的捕获。因此,调度程序的 ForkJoinPool 中的平台线程的数量可能会暂时超过可用处理器的数量。可以使用系统属性 jdk.viralThreadScheduler.maxPoolSize 调优调度程序可用的最大平台线程数。

有两种情况下,在阻塞操作期间无法卸载虚拟线程,因为它被固定在其载体上:

  • 当它在同步块或方法内执行代码时,或

  • 当它执行本机方法或外部函数时。

固定并不会导致应用程序不正确,但它可能会妨碍应用程序的可伸缩性。如果虚拟线程在固定时执行阻塞操作(如 I/O 或 BlockingQueue.take () ) ,那么它的载体和底层操作系统线程将在操作期间被阻塞。长时间的频繁固定会通过捕获运营商而损害应用程序的可伸缩性。

调度程序不会通过扩展其并行性来补偿固定。相反,可以通过修改频繁运行的同步块或方法来避免频繁和长时间的固定,并保护潜在的长 I/O 操作来使用 java.util.concurrent.locks.ReentrantLock。不需要替换不常使用的同步块和方法(例如,只在启动时执行)或保护内存操作的同步块和方法。一如既往,努力保持锁定策略的简单明了。

新的诊断有助于将代码迁移到虚拟线程,以及评估是否应该使用 java.util.concurrent lock 替换同步的特定用法:

  • 当线程在固定时阻塞时,会发出 JDK JFR事件。

  • 当线程在固定时阻塞时,系统属性 jdk.tracePinnedThreads 触发堆栈跟踪。使用-Djdk.tracePinnedThreads = full 运行会在线程被固定时打印一个完整的堆栈跟踪,并突出显示保存监视器的本机框架和框架。使用-Djdk.tracePinnedThreads = short 将输出限制为有问题的帧。

内存使用和垃圾回收

虚拟线程的堆栈作为堆栈块对象存储在 Java 的垃圾回收堆中。堆栈随着应用程序的运行而增长和缩小,这既是为了提高内存效率,也是为了容纳任意深度的堆栈(直到 JVM 配置的平台线程堆栈大小)。这种效率支持大量的虚拟线程,因此服务器应用程序中每个请求一个线程的风格可以继续存在。

在上面的第二个例子中,回想一下,一个假设的框架通过创建一个新的虚拟线程并调用 handle 方法来处理每个请求; 即使它在深度调用堆栈的末尾调用 handle (在身份验证、事务处理等之后) ,handle 本身也会产生多个虚拟线程,这些虚拟线程只执行短暂的任务。因此,对于每个具有深层调用堆栈的虚拟线程,都会有多个具有浅层调用堆栈的虚拟线程,这些虚拟线程消耗的内存很少。

通常,虚拟线程所需的堆空间和垃圾收集器活动的数量很难与异步代码的数量相比较。一百万个虚拟线程至少需要一百万个对象,但是共享一个平台线程池的一百万个任务也需要一百万个对象。此外,处理请求的应用程序代码通常跨 I/O 操作维护数据。每个请求一个线程的代码可以将这些数据保存在本地变量中:

  • 这些本地变量存储在堆中的虚拟线程堆栈中

  • 异步代码必须将这些数据保存在从管道的一个阶段传递到下一个阶段的堆对象中

一方面,虚拟线程需要的堆栈帧布局比紧凑对象更浪费; 另一方面,虚拟线程可以在许多情况下变异和重用它们的堆栈(取决于低级 GC 交互) ,而异步管道总是需要分配新对象,因此虚拟线程可能需要更少的分配。

总的来说,每个请求线程与异步代码的堆消耗和垃圾收集器活动应该大致相似。随着时间的推移,我们希望使虚拟线程堆栈的内部表示更加紧凑。

与平台线程堆栈不同,虚拟线程堆栈不是 GC 根,所以它们中包含的引用不会被执行并发堆扫描的垃圾收集器(比如 G1)在 stop-the-world 暂停中遍历。这也意味着,如果一个虚拟线程被阻塞,例如 BlockingQueue.take () ,并且没有其他线程可以获得对虚拟线程或队列的引用,那么线程就可以被垃圾收集ーー这很好,因为虚拟线程永远不会被中断或解除阻塞。当然,如果虚拟线程正在运行,或者它被阻塞并且可能被解除阻塞,那么它将不会被垃圾收集。

当前虚拟线程的一个限制是 G1 GC 不支持大型堆栈块对象。如果虚拟线程的堆栈达到区域大小的一半(可能小到512KB) ,那么可能会抛出 StackOverfloError。

具体变化

java.lang.Thread

  • Thread.Builder, Thread.ofVirtual(), 和 Thread.ofPlatform() 是创建虚拟线程和平台线程的新 API,例如:

Thread thread = Thread.ofVirtual().name("duke").unstarted(runnable);
Copy after login

创建一个新的未启动的虚拟线程“ duke”。

  • Thread.startVirtualThread(Runnable) 是创建然后启动虚拟线程的一种方便的方法。

  • Thread.Builder 可以创建线程或 ThreadFactory, 后者可以创建具有相同属性的多个线程。

  • Thread.isVirtual() 测试是否一个线程是一个虚拟的线程。

  • Thread.join 和 Thread.sleep 的新重载接受等待和睡眠时间作为java.time.Duration的实例。

  • 新的 final 方法 Thread.threadId() 返回线程的标识符。现在不推荐使用现有的非 final 方法 Thread.getId() 。

  • Thread.getAllStackTraces() 现在返回所有平台线程的映射,而不是所有线程的映射。

java.lang.Thread API其他方面没有改变。构造器也无新变化。

虚拟线程和平台线程之间的主要 API 差异是:

  • 公共线程构造函数不能创建虚拟线程。

  • 虚拟线程始终是守护进程线程,Thread.setDaemon (boolean)方法不能将虚拟线程更改为非守护进程线程。

  • 虚拟线程有一个固定的 Thread.NORM_PRIORITY 优先级。Thread.setPriority(int)方法对虚拟线程没有影响。在将来的版本中可能会重新讨论这个限制。

  • 虚拟线程不是线程组的活动成员。在虚拟线程上调用时,Thread.getThreadGroup() 返回一个名为“ VirtualThreads”的占位符线程组。The Thread.Builder API 不定义设置虚拟线程的线程组的方法。

  • 使用 SecurityManager 集运行时,虚拟线程没有权限。

  • 虚拟线程不支持 stop(), suspend(), 或 resume()方法。这些方法在虚拟线程上调用时引发异常。

Thread-local variables

虚拟线程支持线程局部变量(ThreadLocal)和可继承的线程局部变量(InheritableThreadLocal) ,就像平台线程一样,因此它们可以运行使用线程局部变量的现有代码。但是,由于虚拟线程可能非常多,所以应该在仔细考虑之后使用线程局部变量。

特别是,不要使用线程局部变量在线程池中共享同一线程的多个任务之间共享昂贵的资源。虚拟线程永远不应该被共享,因为每个线程在其生存期内只能运行一个任务。我们已经从 java.base 模块中移除了许多线程局部变量的使用,以便为虚拟线程做准备,从而减少在使用数百万个线程运行时的内存占用。

此外:

  • The Thread.Builder API 定义了一个在创建线程时选择不使用线程局部变量的方法(a method to opt-out of thread locals when creating a thread)。它还定义了一个方法来选择不继承可继承线程局部变量的初始值( a method to opt-out of inheriting the initial value of inheritable thread-locals)。当从不支持线程局部变量的线程调用时, ThreadLocal.get()返回初始值,ThreadLocal.set(T) 抛出异常。

  • 遗留上下文类加载器( context class loader)现在被指定为像可继承的线程本地一样工作。如果在不支持线程局部变量的线程上调用 Thread.setContextClassLoader(ClassLoader),那么它将引发异常。

Networking

The network API implementation in the java.net and java.nio.channels packages now works with virtual threads: an operation on the virtual thread blocks, for example, establishing a network connection or reading from a socket, freeing the underlying platform threads to do other work.

To allow interruption and cancellation, the blocking I/O methods defined by java.net.Socket, ServerSocket and DatagramSocket are now designated as interruptible when called in a virtual thread: Interrupt blocked on socket The virtual thread will release the thread and close the socket.

Blocking I/O operations on these types of sockets are always interruptible when obtained from an InterruptibleChannel, so this change makes these APIs behave the same as the constructor when obtained from the channel behavior remains consistent.

java.io

The java.io package provides APIs for byte and character streams. The implementation of these APIs is highly synchronous and needs to be changed to avoid being pinned for use in virtual threads.

Under the hood, byte-oriented input/output streams are not specified as thread-safe, nor are the expected behaviors when calling close() while blocking a thread in a read or write method. In most cases, it doesn't make sense to use a specific input or output stream from multiple concurrent threads. Character-oriented readers/writers are also not specified as thread-safe, but they do expose a lock object to subclasses. In addition to being fixed, there are issues and inconsistencies with synchronization in these classes; for example, the stream decoders and encoders used by InputStreamReader and OutputStreamWriter synchronize on stream objects rather than lock objects.

To prevent pinning, the current implementation is as follows:

  • BufferedInputStream, BufferedOutputStream, BufferedReader, BufferedWriter, PrintStream, and PrintWriter now use explicit locks instead of explicit locks when used directly monitor. When these classes are subclassed, they are synchronized as before.

  • Stream decoders and encoders used by InputStreamReader and OutputStreamWriter now use the same locks as the enclosing InputStreamReader or OutputStreamWriter.

Going a step further and eliminating all of these often unnecessary locks is beyond the scope of this article.

Additionally, the initial size of the buffers used by the stream encoders of BufferedOutputStream, BufferedWriter and OutputStreamWriter are now smaller to reduce memory usage when there are many streams or writers in the heap - if there are a million A virtual thread, each thread has a buffer stream on the socket connection, this situation may occur

Recommended study: "java video tutorial"

The above is the detailed content of Summary of Java knowledge points: JDK19 virtual thread. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.im
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!