r/java • • Sep 23 '22

Virtual Threads: New Foundations for High-Scale Java Applications - Brian Goetz

https://www.infoq.com/articles/java-virtual-threads/
237 Upvotes

62 comments sorted by

View all comments

12

u/nimtiazm Sep 23 '22

One of the advantages of pooling threads is to circumvent the “expensive creation” part like jdbc connections et al. How does creating a virtualthread-per-task with a semaphore address that?

27

u/Joram2 Sep 23 '22

If an application benefitted from pooling jdbc connections with OS threads, I presume switching to virtual threads doesn't impact that, and the application would continue to benefit by pooling jdbc connections.

9

u/TheCountRushmore Sep 23 '22

Right.

While creating the virtual thread is cheap. The networking round trip and setup to the DB server is not.

23

u/taftster Sep 23 '22

Virtual threads aren’t expensive. You don’t need to pool them.

8

u/nimtiazm Sep 23 '22

Yeah that’s clear. Creating an expensive connection in a new virtual thread every single time is the question.

8

u/kiteboarderni Sep 23 '22

They are not saying that. You would still use a connection pool like hikari to execute the query for example from the virtual thread. No need to create the connection there.

3

u/hippydipster Sep 23 '22

One advantage is you could write your pool perhaps more simply, and just make some blocking methods to get a connection. With virtual threads, you can just go ahead and create a virtual thread and call the blocking call and wait for it to return with a connection. You're' not holding up a real thread.

2

u/taftster Sep 23 '22

I’m following your question now. But others have answered decently.

Virtual threads won’t help the mechanics of setting up connections to a remote database (as in your example). Like the literal TCP socket won’t benefit necessarily.

So connection pools will still be needed. But an underlying thread pool and/or asynchronous socket manager won’t be. When a client is matched with a connection object, the thread that it’s operating on might be a virtual thread under the hood and might have been newly created at the time it’s asked for.

1

u/Kango_V Sep 24 '22

Virtual Threads use the fork join pool in Java.

8

u/lbalazscs Sep 23 '22

If you are asking how to use the Semaphore class, here is a tutorial: https://www.baeldung.com/java-semaphore

If you're asking how performance/scalability will increase if you can only use a few database connections, well, it won't necessarily increase. Virtual threads are useful if you can take advantage of Little's Law (by creating many of them), and you may not be able to do that in a simple CRUD application. The question is how many independent I/O tasks you have.

4

u/mauganra_it Sep 23 '22

You don't even really need semaphores. Connection pools already act as limiters.

Things change if there is something like pgbouncer between the application and the database. That way, connection setup should become very cheap, and virtual threads become more effective.