Live data from Hacker News

Java Virtual Threads Preview

openjdk.java.net

91–100 of 270 posts

Re: Java Virtual Threads Preview

#91
post #86
post #72

Earlier quoted context omitted.

Kotlin coroutines could take advantage of virtual threads but they still will have the syntatic problem of colored functions.

Kotlin can call regular Java APIs, though. Doesn't have to take the coroutine route.

Yes but then your code isn’t idiomatic/multi platform/whatever. It’s a trade off (and one where I would always chose Java).

Re: Java Virtual Threads Preview

#92
post #86
post #72

Earlier quoted context omitted.

Kotlin coroutines could take advantage of virtual threads but they still will have the syntatic problem of colored functions.

Kotlin can call regular Java APIs, though. Doesn't have to take the coroutine route.

In abstract, yes.

In the real Kotlin world of taking a random Kotlin library and call it from Java, most likely "it depends".

Re: Java Virtual Threads Preview

#93
post #91
post #86

Earlier quoted context omitted.

Kotlin can call regular Java APIs, though. Doesn't have to take the coroutine route.

Yes but then your code isn’t idiomatic/multi platform/whatever. It’s a trade off (and one where I would always chose Java).

I learned an hard lesson in the Borland ecosystem.

Always go with the platforms languages, and the IDEs from the platform owners, even if others are more shinny.

Long term it always pays off to be the turtle, as the platforms move into directions not forseen by the shinny objects, and 3rd party IDEs keep playing catching up with SDK features.

Re: Java Virtual Threads Preview

#94
post #48
post #39

Earlier quoted context omitted.

That would be my guess as well, and I would (casually, sitting in my armchair) expect two previews for such a significant change to the JVM. I'm not expecting it to land fully until Java 20 at least -- but we should absolutely give the previews a try as they come out.

Indeed. Since Java is moving to 2 year LTS cycle, I guess overall plan would be have it as standard feature with JDK-21 LTS. Also most likely with primitive objects and much enhanced pattern matching I feel JDK-21 is going to suck a lot oxygen from other JVM languages.

I actually think it will greatly provide a lot of oxygen to other languages.

Virtual threads, project lilliput and valhalla is likely to be a great benefit for Clojure, which has great thread primitives and also spawn a _lot_ of objects that (mostly) don't care about identity.

Re: Java Virtual Threads Preview

#95

Earlier quoted context omitted.

> NodeJS [...] with its thread-per-request model Node.js doesn't create a thread per request; it's single-threaded with evented I/O. You can use node-cluster to start more than a single thread to saturate multi-core CPUs and load-balance HTTP requests across these, but that doesn't make it thread-per-request.

Suspect they're talking about Java - a lot of frameworks do exactly create a thread per request.

I can't think of any framework that still does one thread per request. Normally there is a a queue of incoming requests and they then get dispatched on a thread pool as threads return to the pool.

The challenge is normally that if any of the threads in the pool, as part of processing a request, needs to itself make an IO call, it will block. Ideally you'd want to park the request processing, return the thread to the pool, pick up the next request, until the IO is done where then on the next thread available from the pool you'd resume that request instead of picking another one. This is what the virtual threads will make really easy I think.

Re: Java Virtual Threads Preview

#96

I am lost here. the non-goal mentioned here are actually worth taking as goals to be fulfilled so that every other language and library ecosystem which runs on JVM will get benefited. Non-Goals:- It is not a goal to change the existing implementation of platform threads, that represent Operating System (OS) threads. It is not a goal to automatically convert existing thread construction to virtual threads. It is not a…

backwards compatibility?

Re: Java Virtual Threads Preview

#98

I am lost here. the non-goal mentioned here are actually worth taking as goals to be fulfilled so that every other language and library ecosystem which runs on JVM will get benefited. Non-Goals:- It is not a goal to change the existing implementation of platform threads, that represent Operating System (OS) threads. It is not a goal to automatically convert existing thread construction to virtual threads. It is not a…

Those all make sense:

- Native threads are great. They have a lot of uses, why touch them

- Automatic conversion will break a lot of stuff and eliminate some of the benefits of native threads. Being able to use an API/implementation that is *almost* the same is a huge beneift

- The memory model is a completely separate thing that Java worked on for decades. You don't want to touch that and you don't need to

- Inter-thread communication is a separate thing. There's no reason to go after that. Same is true for the data parallelism stuff.

The focus should be on hitting the 98% of what matters and getting it out without breaking everything we already have.

Re: Java Virtual Threads Preview

#99

Is this like .NET tasks? If so, what’s the async story here? Does it involve function colouring like in .NET?

The doco explains it quite well, so I won't repeat it here. It's solving the same problem that async does, but it does it with virtual threads instead. The idea is that functions aren't coloured, and that normal threaded code will "just work". I see some benefits of this approach, but I feel that what all of the solutions (Java, C#, Rust, etc...) are missing is structured concurrency [1], without which madness and el…

Project Loom will include structured concurrency. But most of that will come out in releases after virtual threads.

If you read the JEP though, you'll see that Executors are auto-closable now, which means you can use try-with-resources to wait for all spawned threads to stop before continuing execution.

Re: Java Virtual Threads Preview

#100

There is a spectrum of solutions for dealing with slow I/O in programming languages (well, all I/O is best assumed to be slow I/O). At the two ends of the spectrum are: - continuation-passing style (CPS), hand-coded or compiler - preemptive threading / processes In between lie various solutions, like async/await (closer to CPS), and green threads (closer to preemptive threading). The key difference between the two en…

This is partially right. But the Java implementation under the hood is still a CPS transformation, so it only allocates the memory that is required for the "virtual stack". Compared to that Go - which has similar semantics - allocates a more classical stack for each goroutine, which can however grow over time.

The CPS approach is certainly more space efficient, but but I'm not sure how much of a difference it really makes in the end. Go seems to be doing well too, after some attempts with linked stack segments and then moving to copying stacks while growing them.

What is interesting is that that some of the CPS like implementations have other performance drawbacks. E.g. since it makes the virtual stack more distributed over memory, the cache efficiency of such an approach might be lower. In Rust one limitation of the CPS approach is that the coroutine state is first stack allocated before being moved onto the heap, and this operation has shown itself to be costly for some applications. So right now I'm not sure if there is any implementation which is superior in all possible benchmarks. But the Java one definitely seems to make a lot of sense for what they want to offer!

Post reply on HN