Live data from Hacker News

Java Virtual Threads: A Case Study

infoq.com

191–199 of 199 posts

Re: Java Virtual Threads: A Case Study

#191

Earlier quoted context omitted.

Each thread adds overhead. Some usage types don’t care, some do. From what I gather virtual threads are an alternative to “callback-hell” (js) or async coloring (python).

> Some usage types don’t care, some do. I suspect if you care about threads overhead, you won't pick Java, because there will be overhead in other areas too > From what I gather virtual threads are an alternative to “callback-hell” (js) or async coloring (python). there is also existing ExecutorService and futures in Java

> there is also existing ExecutorService and futures in Java

Yes, virtual threads are an alternative also to those. (Kind of)

Re: Java Virtual Threads: A Case Study

#192

Earlier quoted context omitted.

> Some usage types don’t care, some do. I suspect if you care about threads overhead, you won't pick Java, because there will be overhead in other areas too > From what I gather virtual threads are an alternative to “callback-hell” (js) or async coloring (python). there is also existing ExecutorService and futures in Java

> there is also existing ExecutorService and futures in Java Yes, virtual threads are an alternative also to those. (Kind of)

And my frustration is that java had that API for 20 years, it is used everywhere and absolutely battle tested, and now they are adding those virtual threads which break third party libs and make JVM more complicated with various degradations in exchange of benefits most will not notice..

Re: Java Virtual Threads: A Case Study

#193
post #92

Earlier quoted context omitted.

When using OS threads, there's no switching when they are waiting for a socket (db connection). The OS knows to wake the thread up only when there's something new to see on the connection.

Both sides of a sleep/awake transition with conventional blocking system calls involve heavyweight context switches: the CPU protection level changes and the thread registers get saved out or loaded back in.

Yes, but these don't happen while waiting.

Re: Java Virtual Threads: A Case Study

#194
post #145

Earlier quoted context omitted.

This is a really unfortunate gotcha that's not at all obvious. Does it kick preemption up a layer to the OS then?

The "not at all obvious" gotcha is described in the documentation near the top, under the heading "What is a Virtual Thread?": https://docs.oracle.com/en/java/javase/21/core/virtual-threa... > Like a platform thread, a virtual thread is also an instance of java.lang.Thread. However, a virtual thread isn't tied to a specific OS thread. A virtual thread still runs code on an OS thread. However, when code running in a v…

As a sibling comment points out, there's nothing in what you quoted that logically implies that blocking I/O is the only reason for a virtual thread to be suspended.

The best info I could find was this blog post:

https://blogs.oracle.com/javamagazine/post/going-inside-java...

"Virtual threads, however, are handled differently than platform threads. None of the existing schedulers for virtual threads uses time slices to preempt virtual threads."

The next handful of paragraphs are also interesting.

Re: Java Virtual Threads: A Case Study

#195

Earlier quoted context omitted.

Both sides of a sleep/awake transition with conventional blocking system calls involve heavyweight context switches: the CPU protection level changes and the thread registers get saved out or loaded back in.

Yes, but these don't happen while waiting.

[deleted]

Re: Java Virtual Threads: A Case Study

#196
post #140

Earlier quoted context omitted.

Greenlets ultimately have to be scheduled onto system threads at the end of the day unless you have a lightweight thread model of some sort supported by the OS, so it’s a little bit misleading depending on how far down the stack you want to think about optimizing for greenlets. You could potentially have a poor implementation of task scheduling for some legacy compatibility reason, however. I guess I’d be curious abo…

Even though yes, in the end you have to map onto system threads, there are still quite a fee things you can do. But this is infeasible for Java, unfortunately. For example, in Erlang the entire VM is built around green threads with a huge amount of guarantees and mechanisms: https://news.ycombinator.com/item?id=40989995 When your entire system is optimized for green threads, the question of "it still needs to map ont…

> When your entire system is optimized for green threads, the question of "it still needs to map onto OS threads" loses its significance

How's that? What about parallelism?

Re: Java Virtual Threads: A Case Study

#197
post #161

Earlier quoted context omitted.

I really don’t think it’s useful to be this nonspecific. You could give an example of what a Java greenlet cannot do or how it cannot be optimized, for example. If your whole point is actually just “I prefer the semantics of BEAM threads”, then just say that.

Those semantics are exactly what cannot be done in Java for many reasons (including legacy code etc.). And yes, those semantics are important, but sadly most people stop at "yay we have green threads now" and then a null pointer exception kills their entire app, or the thread that handles requests, or...

So let’s be clear, your point is that you find the API of non-BEAM greenlets less useful, not that they’re somehow necessarily less efficient. Right?

Re: Java Virtual Threads: A Case Study

#198
post #141
post #137

Earlier quoted context omitted.

Virtual threads could be scheduled pre-emptively but currently the scheduler will wait for some kind of thread sleep to schedule another virtual thread. That's just a scheduler implementation detail and the spec is such that a time slice scheduler could be implemented.

Yes, but the problem is that the spec is such that preemptive blocking doesn't need to be implemented. That means that Java programmers have to be very careful when writing code, lest they block the entire underlying (OS) thread! Again, Go already went through that experience. It was painful. Java should have learned and implemented it from the start

> That means that Java programmers have to be very careful when writing code

From JEP 444:

The scheduler does not currently implement time sharing for virtual threads. Time sharing is the forceful preemption of a thread that has consumed an allotted quantity of CPU time. While time sharing can be effective at reducing the latency of some tasks when there are a relatively small number of platform threads and CPU utilization is at 100%, it is not clear that time sharing would be as effective with a million virtual threads.

Also, in this scenario, i think the current scheduler (ForkJoin Pool) will use managed blocker to compensate those pinned carrier threads.

Re: Java Virtual Threads: A Case Study

#199

Earlier quoted context omitted.

Both sides of a sleep/awake transition with conventional blocking system calls involve heavyweight context switches: the CPU protection level changes and the thread registers get saved out or loaded back in.

Yes, but these don't happen while waiting.

[deleted]
Post reply on HN