Skip to content

Commit 2fee59a

Browse files
committed
refactored TaskScheduler interface to use Futures instead of ForkJoinTasks
1 parent 23a71e5 commit 2fee59a

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

src/main/java/com/mgu/parallel/DefaultTaskScheduler.java renamed to src/main/java/com/mgu/parallel/ForkJoinTaskScheduler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
import java.util.concurrent.RecursiveTask;
88
import java.util.function.Supplier;
99

10-
public class DefaultTaskScheduler implements TaskScheduler {
10+
public class ForkJoinTaskScheduler implements TaskScheduler {
1111

1212
private final ForkJoinPool forkJoinPool;
1313

14-
public DefaultTaskScheduler() {
14+
public ForkJoinTaskScheduler() {
1515
this(ForkJoinPool.commonPool());
1616
}
1717

18-
public DefaultTaskScheduler(final ForkJoinPool forkJoinPool) {
18+
public ForkJoinTaskScheduler(final ForkJoinPool forkJoinPool) {
1919
this.forkJoinPool = forkJoinPool;
2020
}
2121

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package com.mgu.parallel;
22

33
import java.util.concurrent.ExecutionException;
4-
import java.util.concurrent.ForkJoinTask;
4+
import java.util.concurrent.Future;
55
import java.util.function.Supplier;
66

77
/**
8-
* Provides some convenience methods to use the parallelization API of the {@link DefaultTaskScheduler}.
8+
* Provides some convenience methods to use the parallelization API of the {@link ForkJoinTaskScheduler}.
99
* This class relies on the {@code ForkJoinPool.commonPool()} for submitted {@code ForkJoinTask}s.
1010
*
1111
* @author Markus Günther (markus.guenther@gmail.com)
1212
*/
1313
public class Schedulers {
1414

15-
private static final DefaultTaskScheduler SCHEDULER = new DefaultTaskScheduler();
15+
private static final TaskScheduler SCHEDULER = new ForkJoinTaskScheduler();
1616

17-
private static <T> ForkJoinTask<T> task(final Supplier<T> body) {
17+
private static <T> Future<T> task(final Supplier<T> body) {
1818
return SCHEDULER.schedule(body);
1919
}
2020

@@ -27,10 +27,10 @@ public static <A, B, C, D> Tuple4<A, B, C, D> parallel(
2727
final Supplier<B> taskB,
2828
final Supplier<C> taskC,
2929
final Supplier<D> taskD) throws ExecutionException, InterruptedException {
30-
final ForkJoinTask<A> ta = task(taskA);
31-
final ForkJoinTask<B> tb = task(taskB);
32-
final ForkJoinTask<C> tc = task(taskC);
33-
final ForkJoinTask<D> td = task(taskD);
34-
return new Tuple4<>(ta.join(), tb.join(), tc.join(), td.get());
30+
final Future<A> ta = task(taskA);
31+
final Future<B> tb = task(taskB);
32+
final Future<C> tc = task(taskC);
33+
final Future<D> td = task(taskD);
34+
return new Tuple4<>(ta.get(), tb.get(), tc.get(), td.get());
3535
}
3636
}

src/main/java/com/mgu/parallel/TaskScheduler.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.mgu.parallel;
22

33
import java.util.concurrent.ForkJoinTask;
4+
import java.util.concurrent.Future;
45
import java.util.function.Supplier;
56

67
/**
@@ -11,7 +12,7 @@
1112
*/
1213
public interface TaskScheduler {
1314
/**
14-
* Schedules the execution of the given {@code Supplier}. Yields a {@code ForkJoinTask} of parametric
15+
* Schedules the execution of the given {@code Supplier}. Yields a {@code Future} of parametric
1516
* type {@code T}.
1617
*
1718
* @param body
@@ -20,23 +21,23 @@ public interface TaskScheduler {
2021
* @param <T>
2122
* type of the result
2223
* @return
23-
* instance of {@code ForkJoinTask} that will eventually conclude with a result of type
24+
* instance of {@code Future} that will eventually conclude with a result of type
2425
* {@code T}
2526
*/
26-
<T> ForkJoinTask<T> schedule(Supplier<T> body);
27+
<T> Future<T> schedule(Supplier<T> body);
2728

2829
/**
29-
* Schedules the execution of the given {@code Runnable}. Yields a {@code ForkJoinTask} of parametric
30+
* Schedules the execution of the given {@code Runnable}. Yields a {@code Future} of parametric
3031
* type {@code Void}. This variant of {@code schedule} should be used if the given {@code Runnable} closes
3132
* over its input data and the input data is the target as well.
3233
*
3334
* @param body
3435
* a procedure that consumes no input arguments and returns no result either, scheduled for
3536
* later execution
3637
* @return
37-
* instance of {@code ForkJoinTask} that will eventually conclude
38+
* instance of {@code Future} that will eventually conclude
3839
*/
39-
ForkJoinTask<Void> schedule(Runnable body);
40+
Future<Void> schedule(Runnable body);
4041

4142
/**
4243
* Executes both given {@code Supplier}s in parallel. Waits for termination of both and returns their

0 commit comments

Comments
 (0)