Class StructuredConcurrency
ExecutorService
(preferably with virtual threads). For example:
var fanout = new StructuredConcurrency();
Result result = fanout.concurrently(
() -> ListFoos(),
() -> listBars(),
(foos, bars) -> ...);
If you need to customize the virtual threads or the executor, you can use any custom ExecutorService like:
ExecutorService executor = ...;
var fanout = new StructuredConcurrency(executor);
Result result = fanout.concurrently(...);
If using a virtual thread executor, it's safe to define StructuredConcurrency as a static final constant because it's stateless as long as the executor is stateless.
- Since:
- 8.0
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Deprecated.Function to join two results from concurrent operations.static interface
Deprecated.Function to join three results from concurrent operations.static interface
Deprecated.Function to join four results from concurrent operations.static interface
Deprecated.Function to join five results from concurrent operations. -
Constructor Summary
ConstructorDescriptionDeprecated.Constructor using the default virtual thread executor to run the concurrent operations.StructuredConcurrency
(ExecutorService executor) Deprecated.Constructor usingexecutor
to run concurrent operations. -
Method Summary
Modifier and TypeMethodDescription<A,
B, R, X extends Throwable>
Rconcurrently
(Supplier<A> a, Supplier<B> b, StructuredConcurrency.Join2<? super A, ? super B, R, X> join) Deprecated.Runsa
andb
concurrently in their own virtual threads.<A,
B, C, R, X extends Throwable>
Rconcurrently
(Supplier<A> a, Supplier<B> b, Supplier<C> c, StructuredConcurrency.Join3<? super A, ? super B, ? super C, R, X> join) Deprecated.Runsa
,b
andc
concurrently in their own virtual threads.<A,
B, C, D, R, X extends Throwable>
Rconcurrently
(Supplier<A> a, Supplier<B> b, Supplier<C> c, Supplier<D> d, StructuredConcurrency.Join4<? super A, ? super B, ? super C, ? super D, R, X> join) Deprecated.Runsa
,b
,c
andd
concurrently in their own virtual threads.<A,
B, C, D, E, R, X extends Throwable>
Rconcurrently
(Supplier<A> a, Supplier<B> b, Supplier<C> c, Supplier<D> d, Supplier<E> e, StructuredConcurrency.Join5<? super A, ? super B, ? super C, ? super D, ? super E, R, X> join) Deprecated.Runsa
,b
,c
,d
ande
concurrently in their own virtual threads.<A,
B, R, X extends Throwable>
Runinterruptibly
(Supplier<A> a, Supplier<B> b, StructuredConcurrency.Join2<? super A, ? super B, R, X> join) Deprecated.Runsa
andb
concurrently and uninterruptibly in their own virtual threads.<A,
B, C, R, X extends Throwable>
Runinterruptibly
(Supplier<A> a, Supplier<B> b, Supplier<C> c, StructuredConcurrency.Join3<? super A, ? super B, ? super C, R, X> join) Deprecated.Runsa
,b
andc
concurrently and uninterruptibly in their own virtual threads.<A,
B, C, D, R, X extends Throwable>
Runinterruptibly
(Supplier<A> a, Supplier<B> b, Supplier<C> c, Supplier<D> d, StructuredConcurrency.Join4<? super A, ? super B, ? super C, ? super D, R, X> join) Deprecated.Runsa
,b
,c
andd
concurrently and uninterruptibly in their own virtual threads.<A,
B, C, D, E, R, X extends Throwable>
Runinterruptibly
(Supplier<A> a, Supplier<B> b, Supplier<C> c, Supplier<D> d, Supplier<E> e, StructuredConcurrency.Join5<? super A, ? super B, ? super C, ? super D, ? super E, R, X> join) Deprecated.Runsa
,b
,c
,d
ande
concurrently and uninterruptibly in their own virtual threads.
-
Constructor Details
-
StructuredConcurrency
public StructuredConcurrency()Deprecated.Constructor using the default virtual thread executor to run the concurrent operations.Fails if runtime is lower than Java 21.
-
StructuredConcurrency
Deprecated.Constructor usingexecutor
to run concurrent operations. Note that ifexecutor
doesn't use virtual threads, it can cause throughput issues by blocking in one of the platform threads.Useful if your project needs specific setting (such as thread name) for the threads . It's strongly recommended to use
Thread.ofVitual()
for the thread factory because otherwise you risk blocking the platform threads.
-
-
Method Details
-
concurrently
public <A,B, R concurrentlyR, X extends Throwable> (Supplier<A> a, Supplier<B> b, StructuredConcurrency.Join2<? super A, ? super B, throws InterruptedException, XR, X> join) Deprecated.Runsa
andb
concurrently in their own virtual threads. After all of the concurrent operations return successfully, invoke thejoin
function on the results in the caller's thread.For example:
StructuredConcurrency fanout = using(executor); Result result = fanout.concurrently( () -> fetchArm(), () -> fetchLeg(), (arm, leg) -> new Result(arm, leg));
Exceptions thrown by these concurrent suppliers are expected to be propagated through exception tunneling (wrapped in a special unchecked exception) and handled by the caller of this method.
- Throws:
InterruptedException
- if the current thread is interrupted while waiting for the concurrent operations to complete. The unfinished concurrent operations will be canceled.RuntimeException
- wrapping the original exception from the virtual thread if any concurrent operation failedX
- thrown by thejoin
function
-
concurrently
public <A,B, R concurrentlyC, R, X extends Throwable> (Supplier<A> a, Supplier<B> b, Supplier<C> c, StructuredConcurrency.Join3<? super A, ? super B, throws InterruptedException, X? super C, R, X> join) Deprecated.Runsa
,b
andc
concurrently in their own virtual threads. After all of the concurrent operations return successfully, invoke thejoin
function on the results in the caller's thread.For example:
StructuredConcurrency fanout = using(executor); Result result = fanout.concurrently( () -> fetchHead(), () -> fetchArm(), () -> fetchLeg(), (head, arm, leg) -> new Result(head, arm, leg));
Exceptions thrown by these concurrent suppliers are expected to be propagated through exception tunneling (wrapped in a special unchecked exception) and handled by the caller of this method.
- Throws:
InterruptedException
- if the current thread is interrupted while waiting for the concurrent operations to complete. The unfinished concurrent operations will be canceled.RuntimeException
- wrapping the original exception from the virtual thread if any concurrent operation failedX
- thrown by thejoin
function
-
concurrently
public <A,B, R concurrentlyC, D, R, X extends Throwable> (Supplier<A> a, Supplier<B> b, Supplier<C> c, Supplier<D> d, StructuredConcurrency.Join4<? super A, ? super B, throws InterruptedException, X? super C, ? super D, R, X> join) Deprecated.Runsa
,b
,c
andd
concurrently in their own virtual threads. After all of the concurrent operations return successfully, invoke thejoin
function on the results in the caller's thread.For example:
StructuredConcurrency fanout = using(executor); Result result = fanout.concurrently( () -> fetchHead(), () -> fetchShoulder(), () -> fetchArm(), () -> fetchLeg(), (head, shoulder, arm, leg) -> new Result(head, shoulder, arm, leg));
Exceptions thrown by these concurrent suppliers are expected to be propagated through exception tunneling (wrapped in a special unchecked exception) and handled by the caller of this method.
- Throws:
InterruptedException
- if the current thread is interrupted while waiting for the concurrent operations to complete. The unfinished concurrent operations will be canceled.RuntimeException
- wrapping the original exception from the virtual thread if any concurrent operation failedX
- thrown by thejoin
function
-
concurrently
public <A,B, R concurrentlyC, D, E, R, X extends Throwable> (Supplier<A> a, Supplier<B> b, Supplier<C> c, Supplier<D> d, Supplier<E> e, StructuredConcurrency.Join5<? super A, ? super B, throws InterruptedException, X? super C, ? super D, ? super E, R, X> join) Deprecated.Runsa
,b
,c
,d
ande
concurrently in their own virtual threads. After all of the concurrent operations return successfully, invoke thejoin
function on the results in the caller's thread.For example:
StructuredConcurrency fanout = using(executor); Result result = fanout.concurrently( () -> fetchHead(), () -> fetchShoulder(), () -> fetchArm(), () -> fetchLeg(), () -> fetchFeet(), (head, shoulder, arm, leg, feet) -> new Result(head, shoulder, arm, leg, feet));
Exceptions thrown by these concurrent suppliers are expected to be propagated through exception tunneling (wrapped in a special unchecked exception) and handled by the caller of this method.
- Throws:
InterruptedException
- if the current thread is interrupted while waiting for the concurrent operations to complete. The unfinished concurrent operations will be canceled.RuntimeException
- wrapping the original exception from the virtual thread if any concurrent operation failedX
- thrown by thejoin
function
-
uninterruptibly
public <A,B, R uninterruptiblyR, X extends Throwable> (Supplier<A> a, Supplier<B> b, StructuredConcurrency.Join2<? super A, ? super B, throws XR, X> join) Deprecated.Runsa
andb
concurrently and uninterruptibly in their own virtual threads. After all of the concurrent operations return successfully, invoke thejoin
function on the results in the caller's thread.For example:
StructuredConcurrency fanout = using(executor); Result result = fanout.uninterruptibly( () -> fetchArm(), () -> fetchLeg(), (arm, leg) -> new Result(arm, leg));
Exceptions thrown by these concurrent suppliers are expected to be propagated through exception tunneling (wrapped in a special unchecked exception) and handled by the caller of this method.
- Throws:
RuntimeException
- wrapping the original exception from the virtual thread if any concurrent operation failedX
- thrown by thejoin
function
-
uninterruptibly
public <A,B, R uninterruptiblyC, R, X extends Throwable> (Supplier<A> a, Supplier<B> b, Supplier<C> c, StructuredConcurrency.Join3<? super A, ? super B, throws X? super C, R, X> join) Deprecated.Runsa
,b
andc
concurrently and uninterruptibly in their own virtual threads. After all of the concurrent operations return successfully, invoke thejoin
function on the results in the caller's thread.For example:
StructuredConcurrency fanout = using(executor); Result result = fanout.uninterruptibly( () -> fetchHead(), () -> fetchArm(), () -> fetchLeg(), (head, arm, leg) -> new Result(head, arm, leg));
Exceptions thrown by these concurrent suppliers are expected to be propagated through exception tunneling (wrapped in a special unchecked exception) and handled by the caller of this method.
- Throws:
RuntimeException
- wrapping the original exception from the virtual thread if any concurrent operation failedX
- thrown by thejoin
function
-
uninterruptibly
public <A,B, R uninterruptiblyC, D, R, X extends Throwable> (Supplier<A> a, Supplier<B> b, Supplier<C> c, Supplier<D> d, StructuredConcurrency.Join4<? super A, ? super B, throws X? super C, ? super D, R, X> join) Deprecated.Runsa
,b
,c
andd
concurrently and uninterruptibly in their own virtual threads. After all of the concurrent operations return successfully, invoke thejoin
function on the results in the caller's thread.For example:
StructuredConcurrency fanout = using(executor); Result result = fanout.uninterruptibly( () -> fetchHead(), () -> fetchShoulder(), () -> fetchArm(), () -> fetchLeg(), (head, shoulder, arm, leg) -> new Result(head, shoulder, arm, leg));
Exceptions thrown by these concurrent suppliers are expected to be propagated through exception tunneling (wrapped in a special unchecked exception) and handled by the caller of this method.
- Throws:
RuntimeException
- wrapping the original exception from the virtual thread if any concurrent operation failedX
- thrown by thejoin
function
-
uninterruptibly
public <A,B, R uninterruptiblyC, D, E, R, X extends Throwable> (Supplier<A> a, Supplier<B> b, Supplier<C> c, Supplier<D> d, Supplier<E> e, StructuredConcurrency.Join5<? super A, ? super B, throws X? super C, ? super D, ? super E, R, X> join) Deprecated.Runsa
,b
,c
,d
ande
concurrently and uninterruptibly in their own virtual threads. After all of the concurrent operations return successfully, invoke thejoin
function on the results in the caller's thread.For example:
StructuredConcurrency fanout = using(executor); Result result = fanout.uninterruptibly( () -> fetchHead(), () -> fetchShoulder(), () -> fetchArm(), () -> fetchLeg(), () -> fetchFeet(), (head, shoulder, arm, leg, feet) -> new Result(head, shoulder, arm, leg, feet));
Exceptions thrown by these concurrent suppliers are expected to be propagated through exception tunneling (wrapped in a special unchecked exception) and handled by the caller of this method.
- Throws:
RuntimeException
- wrapping the original exception from the virtual thread if any concurrent operation failedX
- thrown by thejoin
function
-
Fanout
instead.