InvokeAny is a method to start multiple threads at once. The result of the first thread that finishes will be returned to the caller. In this article, we will explore the different invokeAny methods you can use in Java 19 and beyond. We will first cover two new ways that are using virtual threads. The last example will use platform threads.
InvokeAny with virtual threads
The first example we are going to look at uses virtual threads. We have a try-with-recourse statement that creates an executor that creates a virtual thread for each task. At line 9, the invokeAny method is called with a list of tasks.
InvokeAny with structured concurrency
Structured concurrency is a new way of managing the lifetime of threads in Java. The lifetime is managed through a StructuredTaskScope. There are mainly two scopes, one with InvokeAny behavior and one with InvokeAll behavior. For this article, we are going to implement InvokeAny. To make a StructuredTaskScope that stops the threads when the first one finishes, we need to call the ShutdownOnSuccess method.
We need to call the fork method to add a task for the scope to execute. So, after starting all the tasks we wanted, two in this example. Then we call the join method on the scope, and this method will block till the first thread is finished. After the join is done we can get the result from the scope.
InvokeAny with platform threads
Platform threads are the threads we know from previous Java versions. These threads are tightly coupled to threads that are managed by the operating system. The example below looks like the first example that uses virtual threads because we use an ExecutorService. This time the ExecutorService we are using is a CachedThreadPool. This pool creates a new thread when needed but will reuse threads that have already finished their task.
This article looked at three ways of implementing the invokeAny method. We first used ExecutorService, which creates virtual threads for each task it needs to perform. After that, we saw how to make a StructuredTaskScope that behaves like an invokeAny method. The last example we saw used platform threads we are already familiar with from previous Java versions.
More about virtual threads in Java: