Trying out virtual threads

Daestrum

Expert
Licensed User
Longtime User
I have been playing around with the new virtual threads in java 19.

I created a simple webserver (java in-built hhtpserver not Jserver) and told it to use virtual threads for the handler.

Even in a UI app I can't get it to slow the main thread down.

It will happily use 10,000 threads and not a single warn or error, and the UI carries on as normal.

I did have to make some modifications to be able to use the preview features of Java 19 ( Erel I never touched the IDE at all - I respect your work ).

So far I am really impressed how they work - a simple test I ran 10,000 threads, each sleeping for 1 second, completed in 1.6 seconds.

Off to play some more ( the new features have gotten me coding again :) )
 

JohnC

Expert
Licensed User
Longtime User
I am trying to keep a foreground service running in one of my apps.

Can this be used in some way to help do that?

Maybe like start multiple threads doing the needed function, so even if the OS kills some of the threads due to low memory/CPU conditions, other threads will still keep running?
 

Daestrum

Expert
Licensed User
Longtime User
From what I have found thus far is it's the JVM that controls the threads, suspending and resuming them as it needs. I still have only scratched the surface so far as to what they are suited too.
Not sure what happens if one thread gets killed, I suspect the jvm will continue live threads and probably report a killed one.
 

JohnC

Expert
Licensed User
Longtime User
From what I have found thus far is it's the JVM that controls the threads, suspending and resuming them as it needs. I still have only scratched the surface so far as to what they are suited too.
Not sure what happens if one thread gets killed, I suspect the jvm will continue live threads and probably report a killed one.
When playing with it, if you do find out any new info that could help with this issue, please let me know :)
 

Daestrum

Expert
Licensed User
Longtime User
Is it for B4A or B4J, as I am only testing in B4J at present, as it was easier to get it working in the IDE.
 

JohnC

Expert
Licensed User
Longtime User
B4A, so I am hoping if it can help, that it can be adaptable to B4A.
 

agraham

Expert
Licensed User
Longtime User
Can this be used in some way to help do that?
No. Virtual threads are intended for uses where many, possibly thousands of threads, are present but are usually idle as in most server applications. They provide no performance advantage but reduce memory usage per thread by being lightweight in memory use so allowing many more threads to exist. Usually a real thread is quite heavyweight and expensive in memory and CPU time to create - hence the normal use of a thread pool.

The JVM runs the virtual threads by mapping them onto real threads when they are required to run. For uses where a thread does a lot of work without idling they provide no advantage over using the conventional thread pool - but possibly no disadvantages either.
 
Top