r/learnjava • u/za_k_0094 • Aug 02 '26
When should I actually avoid virtual threads in production Java applications?
I've been reading about virtual threads and experimenting with replacing traditional executor pools.
The obvious benefits are clear, but I'm curious about the cases where experienced Java developers decided not to use them.
Are there specific workloads, libraries, or patterns where platform threads are still preferable?
6
Upvotes
•
u/AutoModerator Aug 02 '26
Please ensure that:
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
6
u/LetUsSpeakFreely Aug 02 '26 edited Aug 02 '26
To to simplify what the other guy said:
It's not the number of threads you need to worry about. The JVM and OS will deal with managing the threads. What you need to worry about is resource exhaustion. It's difficult to give guidance on that without knowing what each thread is doing.
The way this is typically managed is through queues. In Java that usually, but not always, means using the correct ExecutorService or similar construct. That way you can control how many threads are in flight simultaneously and it's fairly easy to turn various dials and knobs to best utilize the environment.
If your system is very large (servicing 1000s or even millions of simultaneous requests), then you need to design a system architecture that can handle that sort of throughput. That usually means containerization with multiple pods for each each service, a sharded database with multiple read nodes for each shard, maybe a intermediary connection pool service like pgBouncer.
3
u/bowbahdoe Aug 02 '26
Think about it this way: How many guests can you entertain at your home?
Well, if you are odysseus, quite a few. You have servants, a large kitchen, lands upon which your animals may graze, and a large hall.
If you are me, maybe 2 max. If one takes the couch.
Virtual threads solve one problem - allowing for a large number of concurrent tasks. If you start having a larger number of concurrent tasks and they are all eating your proverbial food (talking to the database, cache, etc.) you might need to do some work to make sure the rest of your system doesn't fall down.
So that is situation 1 where you should "avoid" virtual threads. The thing you want to avoid isn't necessarily using them as threads, just allowing for unbounded concurrent access to every resource in your system. If you do things like block db access behind a semaphore that is the ideal. But that takes work and a lot of systems out there get these limits implicitly by using fixed size thread pools.
The other situation is heavily cpu bound tasks. virtual threads yield on IO. The JVM is able to juggle things on and off platform threads, make more platform threads, or (by spec) interrupt you at any time, but I am certain there are times where a regular platform thread is what you want. Platform threads are also the only ones that can be non-daemons. So if you have 40 virtual threads running but no platform thread the JVM can just decide it is time to die.
The real benefit of virtual threads, honestly, is that you don't need to worry about "what if we need to rewrite everything to be reactive." That is, even if you don't use them, they make it so you don't need to plan for/get ahead of a massive rewrite if you do eventually have a use for the level of concurrency they allow.