Currently in Java, threads are quite expensive, being mapped directly one-to-one to host OS threads. They impact both memory usage and CPU usage. So we usually limit the number of threads to a few, often roughly the number of cores or so. So increasing/decreasing the size of the thread pool is not likely to make sense nowadays.
Virtual threads in Project Loom
Sounds like your scenario is ideal for the virtual threads (fibers) coming from the Project Loom project. Many virtual threads are mapped to a single host OS thread.
In today's Java threading, if the Java code blocks, the host OS thread blocks. No further work is performed on that thread. With virtual threads, when the Java code blocks the virtual thread is dismounted from its assigned host OS thread, and “parked”. When that code eventually returns, and therefore needs further execution, it is mounted onto another host OS thread. This parking and mounting of virtual threads is much faster than blocking/unblocking of host OS threads. Virtual threads have much less impact on memory and and CPU. So we can have thousands, or even millions, of threads running simultaneously on conventional hardware.
In your scenario where your work load may go up or down, the virtual thread facility in Project Loom will automatically manage the thread pool and scheduling. You should see vastly improved throughput with no effort on your part.
A few caveats:
- Cheap threads can still do expensive things. So you may need to manage or limit your particular concurrent tasks to avoid blowing out memory or overburdening other resources.
- Virtual threads only make sense for tasks that block. That means most common Java work. But for entirely CPU bound tasks such as video encoding/decoding with little to no logging, storage I/O, network I/O, etc. you would stick with conventional Java threads.
- There may be some situations where the particular content in your task may prevent the parking while blocked. You may choose to alter your code a bit to enable the virtual thread from being “pinned” to the host OS thread. This may be especially the case with the initial releases of Loom. This situation is fluid right now in pre-release Loom, so we will need to stay informed as to changes.
Virtual threads and other Project Loom features are available as preview features in Java 19, with experimental builds available now.
For more information, see the articles, presentations, and interviews by members of the Project Loom team such as Ron Pressler and Alan Bateman.