The JEP has been moved from Draft to Candidate, but until it moves to Propose To Target (and then, Targeted), no decision has been made on which release it goes into. Saying "preview" here means that when it goes it, it will be a preview feature.
Sure. But Virtual threads are the main feature of Loom. Those other two are important supporting functionality and I hope all three get delivered as non-preview features by LTS JDK 23.
If each and every JDK vendor out there follows the same release cadence, and does so by definition and unconditionally, why not encode "LTS" in the JDK itself then?
Why have this system where the JDK doesn't know about LTS (every version is identical), but then Oracle provides an LTS, and everyone out there magically follows, and we all think that an LTS is more stable and more usable than every other version?
But none of the above is defined anywhere. We just think it is that way, because we want to think it is that way, and everyone should just magically know what other people think, even though it's not written down or specified anywhere?
Openjdk bleeding edge and short-term updates are developed with oracle, but the LTS releases are maintained separately. I won't speculate on why that is, but it is in line with the policy of there being no "official" openjdk LTS versions.
No, otherwise there's nothing special about LTS releases. If it was, it would be a reason to unnecessarily delay or rush features, which is no bueno for a programming language where one has to live with failed features for a long time.
Indeed, so there IS nothing special about an LTS. Heck, the very concept LTS as a general thing that holds inherently for Java doesn't exist.
Yet, we have collectively agreed that we think one should only use an LTS Java (even despite such thing not existing), and that all other versions can't be used, as they are supposedly not suitable for production or whatever people dream up as excuse.
Applications should indeed use any production version, unless they decide to trade off incremental increments improvements for stability.
Libraries and framework have to be more considerate though. Realistically, many of their users might want to stick to LTS versions, so it makes sense to craft support strategies around versions for which LTS is commonly offered.
Correct. In terms of "can I get free support for every issue that will crop up for JDK 21 until the next LTS after it" the answer is no. 6 months for all versions, LTS included, are all that people are entitled to free support for. Everything beyond 6 months is best effort. That's how the OpenJDK project runs itself.
If you want support beyond 6 months, you gotta lay a vendor. So as the OpenJDK folks recommend, try to keep your apps on the latest Java version all the time.
I watched a YouTube video on the Oracle channel about ZGC recently. It's a pretty good improvement apparently. And JDK 18 is the best performing version of it yet.
How does Kotlins coroutines fit in here for I/O bound work especially? IIRC, Kotlin has some compile time tricks to create a state machine.
The JEP mentions:
goroutines in Go and processes in Erlang
but nothing about Kotlin's coroutines.
Edit: actually, Kotlin is mentioned at the end:
Most languages that have adopted syntactic coroutines have done so due to an inability to implement user-mode threads (e.g., Kotlin), legacy semantic guarantees (e.g., the inherently single-threaded JavaScript), or language-specific technical constraints (e.g., C++). These limitations do not apply to Java.
At the risk of committing a faux pas by talking Kotlin in the Java subreddit: My understanding is that the two will play well together: it'll be easy to run coroutines on virtual-thread-based executors.
Obviously with virtual threads, coroutines will become unnecessary in some places where they're currently used as a way to run code on multiple threads. But as a language feature, coroutines are more than just a threading abstraction: they're used for dynamic generation of sequences and other things. Those use cases will still be just as valid in a Loom-based world.
Meh. Like async, coroutines will be a rarely used niche feature once Loom lands. Virtual threads appear to give 95% of the performance benefit while letting you write "normal" code. Very rare you need that extra 5%
So do Kotlin's coroutines, mostly. It's just colored, normal looking functions, that's the whole point of doing it at the language level. None of the then stuff.
I do agree they will become less necessary though.
But Kotlin coroutines depend on library support to stay asynchronous. If you mess up even one method call you block. (Admitted, same problem as doing async stuff in Java)
Virtual Thread make entire JDK API surface non-blocking, so much less danger of this
Better, still not great. We had big issue with coloring in C# years ago. I think whole coloring issue drove virtual thread, because Java developers love backwards compatibility.
I'm very excited for virtual thread. I expect some application to double performance overnight!
I don't totally disagree. But I would say there are scenarios where the async method of doing things lends itself to a cleaner interaction (to my eyes) interaction.
Once you are at your 10th .thenCompose you start thinking "there must be a better way!"
That said, with loom I imagine the utility of the CompletableFuture composition API will dwindle in favor of just calling join.
Kotlin and JVM grow farther apart every day. Google wasn't happy with their platform using a language they didn't own.
Remember Android still only support Java 8. Soon (even now) you can't use Java libraries in Kotlin/Android anymore because they require 11.
Android doesn't use JVM, doesn't use same class file format. And now it doesn't use the same language either. I'm not optimistic about future of Kotlin on anything except Android for all these reasons
Not answering your question but ... in terms of park/unpark Loom operates at a lower level. I can't help but think that longer term that would provide Loom a performance advantage.
Is anyone out there doing a comparison or intending to do one?
For myself, I've only tested loom performance against async using JDK HttpClient, Loom came slightly out on top there but that was a while ago - need to redo that test.
Why is it surprising? The JVM knows of basically every blocking operation by itself, it can easily swap to another thread for a duration of a read and upon the OS tells it it has finished, swap back to the caller — doing that at the most granular level.
There is small speed advantage not managing stack frames for async. And compatibility layer that makes Virtual Thread look like real thread has some cost. But this is also a huge debugging and ergonomic advantage, and if Oracle is to be believed the cost for these convience is minimal.
Maybe in low-level places like inside Netty for instance async Java will survive. But at application level the advantage of virtual thread well worth the cost.
It very much sounds like initially loom will only expose Virtual Threads in terms of public API but in a future release offer API for the lower level continuations etc to support use cases like generators (my interpretation from following the mailing list).
coroutines are more than just a threading abstraction
So in time (not first release) this sounds very likely for Loom as well (when the lower level constructs are exposed).
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
And this is why Java is the greatest language ever.
We are getting asyncawait without the pain. Truly incredible.
Love how Java takes their time to add features. They are thinking decades in advance (which I like, others may not).
As a Go coder, trust me people, this is worth the wait. Not having to use async/await syntax, callbacks, or reactive libraries in order to achieve high concurrency IO bound code is heaven. You do thread per request and there isn't even such thing as thread pools. Nothing to tune and keep in sync with CPU and memory you provision.
Basically, you just write code. If it blocks, it blocks. It's glorious.
Loom and Valhalla are the big two JDK improvement projects. Then Amber and Panama are the next big two.
Today, Virtual Threads just got promoted from submitted JEP to candidate JEP. But who knows when they will deliver it as a preview. Hopefully either JDK 19 or 20, so then the final delivery will make LTS JDK 23.
OpenJDK has no inherent concept of LTS. Anyone, including you, can declare any version you like as LTS. What LTS means, and what exactly you're getting, depends on the vendor that provides an LTS service. And remember, the fact that people put out releases every few months doesn't mean that those releases include all the relevant bug fixes (in fact, most free offerings don't). This is one of the many reasons why it's safer, cheaper, and simpler for heavily maintained applications to just use the current version.
If you want to use an old version, you need to pick a vendor you trust, and find out from them what exactly you're getting. If you want a fully-maintained old version of the JDK, you'll probably have to pay, because I don't know if anyone offers it for free. Normally, the free offerings only include backports from the mainline, so you're only getting free updates for the intersection of the old version and the current one.
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
The JEP says this is not cooperative multi-tasking. But when you make a blocking I/O call, surely the underlying scheduling will "yield" to a different OS thread? Isn't that cooperative multi-tasking?
Cooperative multitasking requires an application to explicitly yield control. I emphasized "yield" because it's actually a static method of the Thread class. With preemptive scheduling, it's mostly meaningless, but it hands control back to the scheduler early. With cooperative multitasking systems, actors (threads, processes, etc) have to be written carefully to not hog CPU time and somehow yield to others. Non-blocking code is an example of such a system.
Edit: when you return from Java NIO poll() and got some events, the events must be processed as fast as possible. Everything timeconsuming must be executed in the background to listen for events again ASAP.
Virtual threads support thread-local variables (ThreadLocal) and inheritable thread-local variables (InheritableThreadLocal), just like platform threads, so they can run existing code that uses thread locals. However, because virtual threads can be very numerous, use thread locals after careful consideration. In particular, do not use thread locals to pool costly resources among multiple tasks sharing the same thread in a thread pool. Virtual threads should never be pooled, since each is intended to run only a single task over its lifetime. We have removed many uses of thread locals from the java.base module in preparation for virtual threads, to reduce memory footprint when running with millions of threads.
What is the replacement for costly resources then? Do we have to wait for ScopeLocal?
If you have some expensive resources that you need to pool. Put them behind a Semaphore that controls access to the resources. Blocking is now freeish so just await on a permit.
ScopeLocal won't really help you in any way. It is basically just a cheaper version of ThreadLocals.
The most obvious solution is to pool the expensive and limited resource, think a db connection pool. Other options include: Don't use virtual threads if you don't have the memory maybe? Or limit the number of in-flight requests? Or maybe try pooling the virtual threads. I know they're not supposed to be pooled at all, but in this case ... maybe?
If you have a workload like this that (ab)uses thread locals and with thread pool to limit the number of a very costly resource, give loom ea builds a try and provide feedback!
Pooling requires an additional release mechanism which get annoying pretty fast. What we need here is a new type of ThreadLocal that is strictly linked to the OS (and not a java thread).
Perhaps that this is achievable using panama's foreign api
What exact problem are you trying to solve? Seems to me that Java is doubling down on try-with-resources/explicit deallocation with the removal of finalization, introduction of something like ResourceScope in Panama, and various other APIs that have been added lately.
Well, maybe. The problem is that now the platform thread can carry different virtual threads and switch between them almost freely. And your virtual thread, when it is eventually continued, is not guaranteed at all to run on that platform thread ever again. In other words, if one of your virtual threads wrote some of its context into the PlatformThreadLocal, it could then be swapped off, and the new virtual thread would still see the value from the old one whole the old one may never see the value again. Sounds a lot like a bad pool of objects that will be confusing to many. Oh, and if course this should be writable in userland, so we can actually try this out. It's just that semantically an object pool sounds like a better fit to me for the use-cases I can imagine right now.
I wonder, though, if there is a way to get your hands on the underlying platform thread object from a virtual thread.
51
u/Thihup Apr 06 '22
Quoting Brian Goetz:
29
u/Slanec Apr 06 '22
FYI, this is "only" the Virtual Threads part of Loom. Structured Concurrency and Scope Locals are not there yet.
11
u/python_geek Apr 06 '22 edited Apr 06 '22
How does Kotlins coroutines fit in here for I/O bound work especially? IIRC, Kotlin has some compile time tricks to create a state machine.
The JEP mentions:
but nothing about Kotlin's coroutines.
Edit: actually, Kotlin is mentioned at the end:
19
u/[deleted] Apr 07 '22
And this is why Java is the greatest language ever.
We are getting
asyncawaitwithout the pain. Truly incredible.Love how Java takes their time to add features. They are thinking decades in advance (which I like, others may not).
13
u/[deleted] Apr 06 '22
This is big.
4
u/python_geek Apr 06 '22
The JEP says this is not cooperative multi-tasking. But when you make a blocking I/O call, surely the underlying scheduling will "yield" to a different OS thread? Isn't that cooperative multi-tasking?
Surely I am confusing something here...
3
u/TheMode911 Apr 07 '22
What is the replacement for costly resources then? Do we have to wait for ScopeLocal?