diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2020-12-14 19:29:37 -0400 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2020-12-14 19:29:37 -0400 |
| commit | 9351ea3e97bbe2d348aa17067ccc6267dc7c080f (patch) | |
| tree | dd2269c26161c735d94d8dc83d56e6076c2a155d /base/src/main/java/bjc/utils/funcutils/Strategy.java | |
| parent | 8933de7f646f0565edf700aa2f2fcab06d639855 (diff) | |
| parent | 6dcadc360dafdd12142d53327f44579379a4c9dd (diff) | |
Merge branch 'master' of https://github.com/bculkin2442/bjc-utils2
Diffstat (limited to 'base/src/main/java/bjc/utils/funcutils/Strategy.java')
| -rw-r--r-- | base/src/main/java/bjc/utils/funcutils/Strategy.java | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/base/src/main/java/bjc/utils/funcutils/Strategy.java b/base/src/main/java/bjc/utils/funcutils/Strategy.java new file mode 100644 index 0000000..316879f --- /dev/null +++ b/base/src/main/java/bjc/utils/funcutils/Strategy.java @@ -0,0 +1,81 @@ +package bjc.utils.funcutils; + +import java.util.concurrent.*; +import java.util.function.*; + +/** + * Strategy for dealing with parallel execution. + * + * @author Ben Culkin + * + * @param <Output> The type returned by the tasks. + * + */ +public interface Strategy<Output> extends Function<Callable<Output>, Future<Output>> +{ + /** + * Convert a function into one which operates concurrently, using this strategy. + * + * @param <Input> The type of the function argument. + * + * @param func The type of the function. + * + * @return A function which executes concurrently. + */ + public default <Input> Function<Input, Future<Output>> using(Function<Input, Output> func) + { + return (input) -> this.apply(() -> func.apply(input)); + } + + /** + * A strategy which will run tasks in serial. + * + * @param <Output> The type returned by the task. + * + * @return A strategy which executes things serially. + */ + public static <Output> Strategy<Output> serial() + { + return (call) -> { + FutureTask<Output> task = new FutureTask<>(call); + task.run(); + return task; + }; + } + /** + * A strategy which creates a fresh thread to execute a task on. + * + * @param <Output> The type returned by the task. + * + * @return A strategy which uses threads to create tasks. + */ + public static <Output> Strategy<Output> simpleThread() + { + // I leave this as an example as of what is possible with combinators. + // return (call) -> invoke(introducing( + // () -> new FutureTask<>(call), + // (task, input) -> doWith( + // (FutureTask<Output> tsk) -> + // new Thread(task).start()).apply(task) + // )); + return (call) -> { + FutureTask<Output> task = new FutureTask<>(call); + new Thread(task).start(); + return task; + }; + } + + /** + * A strategy that uses an executor service. + * + * @param <Output> The type returned by the task. + * + * @param svc The executor service to use. + * + * @return A strategy which uses the provided executor. + */ + public static <Output> Strategy<Output> executorService(ExecutorService svc) + { + return svc::submit; + } +} |
