#JavaTuesdayThread
I’ve been patiently waiting for JEP 429 publishing with my fingers crossed for quite a while since JEP 425 appeared in a public space (although my story with Project Loom is longer than you can imagine). So, today’s topic is - extent-local variables. #Java
When we started talking about virtual threads the question we've been asked is whether virtual threads are like the old ones. The answer was YES, however, some techniques used to organize the data flow around platform threads may not be valid (but working) for virtual threads.
By saying “some” we’re mostly pointing to thread locals. As you you know, it’s possible to start over 10M threads for nothing while it’s not the same for platform threads because the JVM, like the other processes, has a thread count limitation it cannot surpass.
Even before virtual threads, thread locals have a few problems like:
- Unconstrained mutability - whoever can get() can do set() either; [1/3]
- Unbound lifecycle - they exist through a thread lifecycle or up until remove() invocation, which can lead to continuous memory leaks; [2/3]
- Expensive inheritance - in fact, thread local isn’t local to one thread, but available to child threads which leads to a significant memory footprint [3/3]
With virtual threads the situation is a bit different, even if a thread local isn’t removed, the cleanup will be done shortly because virtual threads are meant to be short-lived.
But still, thread-local variables have more complexity than is usually needed for sharing data, and significant costs that cannot be avoided.
So, there has to be a way to maintain immutable and inheritable per-thread data for thousands or millions of virtual threads. Any data shared through the thread-bound variable should become unusable once the method that initially shared the data is finished.
The requirement for the technology we've been looking for is to replace thread-local variables that should prevent unconstraint mutability as well as unbound lifetime.
That’s what “extent-local variables” going to be, it will allow data to be safely and efficiently shared between components in a large program without resorting to method arguments. download.java.net/java/early_acc…
Unlike a thread-local variable, an extent-local variable is written once and is then immutable, and is available only for a bounded period during the execution of the thread.
The idea of extent-locals to limit the period of time through which an underlying runnable object can access immutable value: Image
The immutability of extent-local variables means that a caller can use an extent-local variable to reliably communicate a constant value to its callees in the same thread.
However, there are occasions when one of the callees might need to use the same extent-local variable to communicate a different value to its own callees in the thread.
The “Extent-Locals” API proposes two public classes like “ExtentLocal” and “ExtentLocal.Carrier” - the first one designed to bind a value to an extent-local variable while the second one stands for the execution of a callee (through Runnable or Callable).
Interesting that it’s possible to rebind the value of an extent-local variable from another thread, however, the scope of rebound immutable value, again, is limited to the scope of a callee which means that the original value wouldn’t change to whoever will request that value. Image
The best way to showcase the extent-local variables inheriting is to combine them with structured concurrency blocks (JEP 428) because a thread that will allocate a new scope will eventually call a "fork" that will spawn a child thread that will inherit parent extent-locals.
StructuredTaskScope::fork ensures that the binding of the extent-local variable will make in the request-handling thread is automatically visible in the child thread.
The fork/join model offered by StructuredTaskScope means that the value bound by ExtentLocal::where has a determinate lifetime.
It means that an extent-local variable is available to the child thread while it is running, and scope.join() ensures that child threads terminate and thus no longer use it. This avoids the problem of unbounded lifetimes seen when using thread-local variables.
So, you see, extent-local variables are likely to be useful and preferable in many scenarios where thread-local variables are used today like state propagation, but unlike thread locals, extent-local variables are immutable to however consume.
Probably the most important fact is that a thread-pool problem that is specific to thread-locals (the content of a thread-local variable may not be removed at any time) is not a thing to an extent-locals because the lifecycle of an owner virtual thread is meant to be short...
...and virtual threads are not subject to pooling.

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Denis Makogon

Denis Makogon Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @denis_makogon

Aug 2
#JavaTuesdayThread

Today I’d like to speak a bit regarding #Java Flight Recorder (JFR) and how far you can get with it. #OpenJDK #JFR #JMC
So, as you may know, a few releases ago, Java developers were able to create, publish, and react to particular JDK events, also known as JFR.
The first and most important JFR feature is that the whole JDK is pierced with tons of JFR events. You can enable them to get most of the information about what’s going on with the JVM.
Read 13 tweets
Jul 19
It's time for #JavaTuesdayThread!

You know that JDK 19 will be released in 2 months on Sept. 20th. It will bring some major changes to the JDK. Project #Loom is one of those HUGE changes I want to talk about a little bit more. #Java #OpenJDK
JEP 425 introduces Virtual Threads. The most interesting thing is that the JDK was reworked to make it possible to block both platform and virtual threads in different ways.
While thread blocking methods will put on pause the whole platform thread, the virtual thread blocking wouldn't block its underlying carrier thread so it can pick up the segment of another virtual thread to execute.
Read 15 tweets
Mar 9
This is the first war in a modern world where we have more than just one physical frontier. As Ukrainian IT community we started another one - virtual. I’ll explain you how it works here in #Ukraine and abroad
[thread]:
#Ukraine has more than 160K IT specialists: hardware engineers, cloud engineers, software engineers, devops, architects, QA, etc.
We do have IT security folks known as Ukrainian Cyber Alliance (@UCA_ruhate_). They were the first ones who attacked #Russia’s IT infrastructure.
Read 11 tweets
Mar 8
Yesterday I explained you how a war in Ukraine will affect Europe in terms of food.

Today I will explain you how it will affect your comfort [thread]:
I’d you didn’t know - Ukraine average per hour rate is pretty low comparing to European. That’s why we do lots of outsourcing businesses. We have thousands of smart engineers who do work for US/EU businesses.
VM, Mercedes-Daimler, BMW uses Ukraine as an R&D centers.
Read 8 tweets
Aug 25, 2021
The reason why I love and hate #Kubernetes is it actually good platform to host scalable apps. But the road from developing the app to making it scalable is so painful. Here are few-reasons-why-thread:
#Kubernetes in its bare configuration can only host containers and let you talk to them. Include config maps, volumes, RBAC, etc.
If you ever wanted to scale your app on #Kubernetes you probably heard of MetricsServer - it can collect standard metrics like CPU and RAM of every pod/container.

It’s not a part of #k8s distribution - you need to install it separately.
Read 11 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(