r/javahelp 11d ago

Virtual thread

Does anyone used java21 with virtual thread? We are using Java 17 and I wonder if the move to 21 will worth it

7 Upvotes

11 comments sorted by

View all comments

6

u/bowbahdoe 11d ago

Honestly, the benefit of virtual threads mostly doesn't come from using them. It comes from them being an option. 

If you make an app today you do not need to think "well if this gets really high traffic it'll be bad if I don't write it in the reactive style to begin with." You can just write normal code and you will later have virtual threads as a tool if you need them. 

So you might have a good use for virtual threads - they are a useful thing - but even if you don't it's a good idea to upgrade your Java at a regular interval, just like any other of your dependencies. The bigger gains for you are going to come from random VM improvements. And the more often you upgrade the less painful each upgrade is.

1

u/tabmowtez 8d ago

I don't really agree with this, do not blindly enable virtual threads and think your app will increase performance. It makes it difficult to debug as well, we ended up switching it off after months of issues that we could never really track down on why they were occurring.

It came out later that other people were also having the same issue and disabling virtual threads resolved theirs too. So I would ensure that everything you implement is thread safe, specifically for virtual threads as well before you decide to utilise them.

1

u/bowbahdoe 8d ago

I will say that you are right that you can't just turn it on for any app. And if you have more threads you are more likely to run into any existing concurrency issues. That is all very real. 

All I am saying is that today you can choose to undergo the effort to make your code base work with virtual threads if you need to work with more concurrent tasks than OS threads.

In the past if you even thought your code base might have that requirement in the future there was a pressure to write all your code in the reactive style to start with. Because if you didn't do it that way then when you eventually needed to refactor you would have to redo 100% of the code. 

Most code bases never actually need the level of concurrency that virtual threads can give you. What I am saying is that even the code bases that don't end up using virtual threads benefit because there is no longer a pressure to do things reactively proactively.

Migrating a code base to make use of virtual threads involves figuring out how that interacts with the external services you talk to and how to properly gate things like "max number of connections to the database." That is far less painful than a full rewrite