r/java • • Jun 10 '24

[deleted by user]

[removed]

615 Upvotes

594 comments sorted by

View all comments

Show parent comment

2

u/vprise Jun 10 '24

Java's virtual threads are a response to async programming not to go. They aren't green threads they are hybrids which provide the best of both worlds. go routines don't do that and don't perform as well. Furthermore, they are MUCH easier to debug.

Error handling in rust suffers from all the problems in Go. It might be moderately OK for a low level language but if you're trying to build anything sophisticated you need to constantly be aware of errors. Java solves that while still offering a unique feature that forces you to deal with specific exceptions. Furthermore, since Java is jitted exceptions can be practically free. The JIT can eliminate the error check entirely and the cost of the exception can vanish. This can't be done in Rust/Go.

2

u/maumay Jun 10 '24

I believe Goroutines are comparable to virtual threads in that m goroutines are mapped onto n platform threads where the runtime handles the scheduling.

2

u/vprise Jun 11 '24

It doesn't according to this: https://stackoverflow.com/a/24599790/756809

It opens a new thread when blocked which is a very different thing. Virtual threads use the extra CPU cores even when they aren't blocked. That means you get 100% of the capabilities of the hardware from the start and use sophisticated capabilities soon after.

Also "blocked" does some pretty heavy lifting in that definition. Java has the advantage of the JVM which includes both the execution runtime and the libraries. Handling asynchronous cases is something that Java can handle seamlessly without laying the complexity on the developer. That means better scheduling logic and fewer developer errors.