src/main/java/com/github/phantomthief/util/MoreFunctions.java
public static <R, X extends Throwable> R supplyParallel(ForkJoinPool pool,
            ThrowableSupplier<R, X> func) throws X {
        checkNotNull(pool);
        Throwable[] throwable = { null };
        ForkJoinTask<R> task = pool.submit(() -> {
            try {
                return func.get();
            } catch (Throwable e) {
                throwable[0] = e;
                return null;
            }
        });
        R r;
        try {
            r = task.get();
        } catch (ExecutionException | InterruptedException impossible) {
            throw new AssertionError(impossible);
        }
        if (throwable[0] != null) {
            //noinspection unchecked
            throw (X) throwable[0];
        }
        return r;
    }
public static <R, X extends Throwable> R supplyParallel(ForkJoinPool pool,
            ThrowableSupplier<R, X> func) throws X {
        checkNotNull(pool);
        Throwable[] throwable = { null };
        ForkJoinTask<R> task = pool.submit(() -> {
            try {
                return func.get();
            } catch (Throwable e) {
                throwable[0] = e;
                return null;
            }
        });
        R r;
        try {
            r = task.get();
        } catch (ExecutionException | InterruptedException impossible) {
            throw new AssertionError(impossible);
        }
        if (throwable[0] != null) {
            //noinspection unchecked
            throw (X) throwable[0];
        }
        return r;
    }
public static < R , X extends Throwable > R supplyParallel ( ForkJoinPool pool , ThrowableSupplier < R , X > func ) throws X { checkNotNull ( pool ) ; Throwable [ ] throwable = { null } ; ForkJoinTask < R > task = pool . submit ( ( ) -> { try { return func . get ( ) ; } catch ( Throwable e ) { throwable [ 0 ] = e ; return null ; } } ) ; R r ; try { r = task . get ( ) ; } catch ( ExecutionException | InterruptedException impossible ) { throw new AssertionError ( impossible ) ; } if ( throwable [ 0 ] != null ) { //noinspection unchecked throw ( X ) throwable [ 0 ] ; } return r ; }
mainly use for {@link Stream#parallel()} with specific thread pool
see https://stackoverflow.com/questions/21163108/custom-thread-pool-in-java-8-parallel-stream
mainly use for {