Posted in

JEP 481: JDK 23 中范围值 API 的第三个预览版本带来了关键增强_AI阅读总结 — 包阅AI

包阅导读总结

1.

关键词:Scoped Values API、JDK 23、数据共享、线程、性能优化

2.

总结:JEP 481 为 Scoped Values API 在 JDK 23 中的第三次预览带来关键增强。该 API 解决了线程局部变量的问题,能在线程内和线程间共享不可变数据,优化性能,示例展示其优势,它对 Java 中数据共享和应用性能有重要意义。

3.

主要内容:

– JEP 481 与 Scoped Values API

– 原名 Extent-Local Variables (Incubator),已在 JDK 23 完成。

– 经历了从 JDK 20 到 23 的多轮孵化和预览。

– Scoped Values API 的特点

– 可在线程内和线程间共享不可变数据。

– 优于线程局部变量,尤其在大量虚拟线程使用时。

– 新预览对 ScopedValue.callWhere 方法有改变,ScopedValue.getWhere 方法被移除。

– 与线程局部变量的比较

– 线程局部变量有可变性、生命周期无限制、跨线程继承开销大等问题。

– Scoped 值确保数据不可变和限定访问范围,增强安全性和性能。

– 示例说明

– 以 Web 框架为例,对比使用线程局部变量和 Scoped 值的实现。

– 显示 Scoped 值使实现更清洁高效。

– 结论

– Scoped Values API 显著增强 Java 中数据共享方式,与现代并发模型契合,对高并发应用开发重要。

思维导图:

文章地址:https://www.infoq.com/news/2024/07/jep-481-enhanced-scoped-values/?utm_campaign=infoq_content&utm_source=infoq&utm_medium=feed&utm_term=global

文章来源:infoq.com

作者:A N M Bazlur Rahman

发布时间:2024/7/22 0:00

语言:英文

总字数:658字

预计阅读时间:3分钟

评分:87分

标签:Java,范围值 API,JDK 23,并发编程,虚拟线程


以下为原文内容

本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com

JEP 481, Scoped Values (Third Preview), formerly known as Extent-Local Variables (Incubator), has been completed for JDK 23.This JEP offers a third previewwith one change to gain additional experience and feedback from one round of incubation and two rounds of preview, namely: JEP 464, Scoped Values (Second Preview), delivered in JDK 22; JEP 446, Scoped Values (Preview), delivered in JDK 21; and JEP 429, Scoped Values (Incubator), delivered in JDK 20. This feature enables the sharing of immutable data within and across threads. This is preferred to thread-local variables, especially when using large numbers of virtual threads.

The re-preview of the Scoped Values API in JDK 23 introduces a new change to the ScopedValue.callWhere method. The operation parameter of this method is now a functional interface. It allows the Java compiler to infer whether a checked exception might be thrown. Consequently, the ScopedValue.getWhere method is no longer needed and has been removed. This leads to cleaner code and improved performance in frequent data-sharing scenarios.

Scoped values enable a method to share immutable data with its callees and child threads, making it easier to manage and reason about data flow compared to thread-local variables. They offer lower space and time costs, especially when combined with virtual threads (JEP 444) and structured concurrency (JEP 480).

However, thread-local variables, introduced in Java 1.2, have been the traditional method for facilitating data sharing across methods within the same thread. Despite their long-standing use, they come with several drawbacks. One major issue is their unconstrained mutability, allowing any code to change the thread-local variable’s value at any time, which can lead to potential inconsistencies. Another drawback is their unbounded lifetime; values can persist longer than necessary, often resulting in memory leaks if developers forget to call the remove method. Additionally, inheriting thread-local variables across threads adds significant overhead, as each child thread must allocate storage for every thread-local variable previously written in the parent thread, negatively impacting performance.

Scoped values address these issues by ensuring data is immutable and only accessible within a defined scope, thereby enhancing both security and performance.

To illustrate the benefits of scoped values, consider a web framework where context needs to be shared across different methods without explicitly passing it as a parameter. Using thread-local variables, the framework might look like this:

class Framework {  private final static ThreadLocal<FrameworkContext> CONTEXT = new ThreadLocal<>();  void serve(Request request, Response response) {    var context = createContext(request);    CONTEXT.set(context);    Application.handle(request, response);  }  public PersistedObject readKey(String key) {    var context = CONTEXT.get();    var db = getDBConnection(context);    return db.readKey(key);  }}

With scoped values, the implementation becomes cleaner and more efficient:

class Framework {  private final static ScopedValue<FrameworkContext> CONTEXT = ScopedValue.newInstance();  void serve(Request request, Response response) {    var context = createContext(request);    ScopedValue.runWhere(CONTEXT, context, () -> Application.handle(request, response));  }  public PersistedObject readKey(String key) {    var context = CONTEXT.get();    var db = getDBConnection(context);    return db.readKey(key);  }}

Using a thread-local variable avoids the need to pass a FrameworkContext as a method argument when the framework calls user code and when user code calls a framework method back. The thread-local variable serves as a hidden method parameter: a thread that calls CONTEXT.set in Framework.serve and then CONTEXT.get in Framework.readKey will automatically see its own local copy of the CONTEXT variable. In effect, the ThreadLocal field serves as a key that is used to look up a FrameworkContext value for the current thread. On the other hand, using scoped values simplifies this process by eliminating the need for a mutable state and ensuring that the context is only accessible within the defined scope of the runWhere method, providing a more robust and performance-friendly approach.

In conclusion, the Scoped Values API significantly enhances the way data is shared across methods and threads in Java, promoting better coding practices and improving application performance. It aligns well with modern concurrency models, particularly with the advent of virtual threads, making it a critical addition for developers working on highly concurrent applications.