Live data from Hacker News

Java Virtual Threads: A Case Study

infoq.com

171–180 of 199 posts

Re: Java Virtual Threads: A Case Study

#171
post #92

Earlier quoted context omitted.

In large numbers the cost of switching between threads does consume CPU while they're waiting for the database. This is why green threads exist, to have large numbers of in flight work executing over a smaller number of OS threads.

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.

[deleted]

Re: Java Virtual Threads: A Case Study

#172
post #7
post #2

What is the virtual thread / event loop pattern seeking to optimize? Is it context switching? A number of years ago I remember trying to have a sane discussion about “non blocking” and I remember saying “something” will block eventually no matter what… anything from the buffer being full on the NIC to your cpu being at anything less than 100%. Does it shake out to any real advantage?

It's a brave attempt to release the programmer from worrying or even thinking about thread pools and blocking code. Java has gone all in - they even cancelled a non-blocking rewrite of their database driver architecture because why have that if you won't have to worry about blocking code? And the JVM really is a marvel of engineering, it's really really good at what it does, so what team to better pull this off? So f…

Thank you for a A very candid response I enjoyed reading it!

My question is though: Why even do alleged “non-blocking” _at all_? What are people trying to optimize against?

Re: Java Virtual Threads: A Case Study

#173

Earlier quoted context omitted.

> So... What is it seeking to optimize? The goal is to maximize the number of tasks you can run concurrently, while imposing on the developers a low cognitive load to write and maintain the code. > Why did you need a thread pool before but not any more? You still need a thread pool. Except with virtual threads you are no longer bound to run a single task per thread. This is specially desirable when workloads are IO-b…

> why should you block a thread if creating gazillion threads on modern hardware is super cheap why not? I have transparency and debuggability what threads are running, can check stacktrace of each and what are they blocked on. virtual threads adds lots of magic under the hood, and if there will be some bug or lib in your infra with no vthreads support it is absolutely not clear how to debug it.

> if creating gazillion threads on modern hardware is super cheap why not?

Virtual threads are a performance improvement over threads, no matter how cheap to create threads are. Virtual threads run on threads. If threads become cheaper to create, so do virtual threads. They are not mutually exclusive.

Virtual threads are on top of that a developer experience improvement. Code is easier to write and maintain.

Virtual threads improve throughput because the moment a task is waiting for anything like IO, the thread is able to service any other task in the queue.

Re: Java Virtual Threads: A Case Study

#174
post #92

Earlier quoted context omitted.

In large numbers the cost of switching between threads does consume CPU while they're waiting for the database. This is why green threads exist, to have large numbers of in flight work executing over a smaller number of OS threads.

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.

Re: Java Virtual Threads: A Case Study

#175
post #168

Earlier quoted context omitted.

It's been a really long time since I dealt with anything this low level, but in my very limited and ancient experience when people talk about context switching they're talking specifically about the userland process yielding execution back to the kernel so that the processor can be reassigned to a different process/thread. Naively, if the JVM isn't actually yielding control back to the kernel, it has the freedom to d…

When a real thread is allocated from a virtual thread to another, the JVM needs to save into the heap the stack of the first virtual thread and restore from the heap the stack of the second virtual thread, see slide 13 of [1]. This is in fact called mounting/unmounting as already pointed out, and occurs via Java Continuation, but from the JVM perspective this is a context switch. It's called JVM and the V stands for…

[deleted]

Re: Java Virtual Threads: A Case Study

#176

Earlier quoted context omitted.

This is a type of context switch. You are saying dollars are cheaper than money.

It's been a really long time since I dealt with anything this low level, but in my very limited and ancient experience when people talk about context switching they're talking specifically about the userland process yielding execution back to the kernel so that the processor can be reassigned to a different process/thread. Naively, if the JVM isn't actually yielding control back to the kernel, it has the freedom to d…

swapcontext(3) is a userland context switch, and is named so.

Re: Java Virtual Threads: A Case Study

#177
post #143

Earlier quoted context omitted.

Why wouldn't 4k virtual threads lead to the same problems?

Because they don't create 4k real threads, and can be scheduled on n=CPU Cores OS threads

4k "real" threads can also be scheduled on 4 CPU cores. What's the difference?

Re: Java Virtual Threads: A Case Study

#178
post #166
post #142

Earlier quoted context omitted.

If you need to be explicit about thread contexts because you're using a thread that's bound to some other runtime (say, a GL Context) or you simply want to use a single thread for synchronization like is common in UI programming with a Main/UI Thread, async/await does quite well. The async/await sugar ends up being a better devx than thread locking and implicit threading just doesn't cut it. In Java they're working o…

What’s stopping you from using a single thread for synchronization?

You can use virtual threads running on a single OS thread and that will work but then everything will be on that one thread. You'll have synchronization but you'll also always be blocking on that one thread as well.

Async/await is able to achieve good UX around explicitly defining what goes on your Main thread and what goes elsewhere. Its trivial to mix UI thread and background thread code by bouncing between synchronization contexts as needed.

When the threading model is implicit its impossible to have this control.

Re: Java Virtual Threads: A Case Study

#179

Earlier quoted context omitted.

> why should you block a thread if creating gazillion threads on modern hardware is super cheap why not? I have transparency and debuggability what threads are running, can check stacktrace of each and what are they blocked on. virtual threads adds lots of magic under the hood, and if there will be some bug or lib in your infra with no vthreads support it is absolutely not clear how to debug it.

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

Re: Java Virtual Threads: A Case Study

#180
post #160
post #107

Earlier quoted context omitted.

It's foolish to say that green threads are strictly better and ignore async/await as something outdated. It can do a lot that green threads can't. For example, you can actually share a thread with another runtime. Cooperative threading allows for implicit critical sections that can be cumbersome in preemptive threading. Async/await and virtual threads are solving different problems. > What is perhaps worse is that C#…

> It's foolish to say that green threads are strictly better and ignore async/await as something outdated I’m not sure I said outdated, but I can see what you mean by how I called Javas approach “more modern”. What I should have called Javas approach was “correctly designed”. C#’s async/await isn’t all terrible as you point out, but it’s designed wrong from the bottom up because computation should always be blocking…

What do you think about the Structured Concurrency library Java is working with things like fork() and join()? Is that incorrectly designed? Why do you think there's a call for that if virtual threads serves every use case?
Post reply on HN