lap*_*nis 5 java lambda callable
我正在尝试使用泛型(我第一次尝试使用泛型)并使用ExecutorService来实现“ TaskExecutor”。
这是我的“ TaskExecutor”类:
public class ExecuteAlerterTask<T> {
public List<T> process(String executorName, Callable<T> task) throws ExecutionException, InterruptedException {
final ThreadFactory threadFactory = new ThreadFactoryBuilder()
.setNameFormat(executorName + "-%d")
.setDaemon(true)
.build();
ExecutorService executor = Executors.newFixedThreadPool(10, threadFactory);
Collection<Future<T>> futures = new ArrayList<>();
IntStream.range(1, 10).forEach(i -> {
Future<T> future = executor.submit(task);
futures.add(future);
});
List<T> result = new ArrayList<>();
for (Future<T> f : futures) {
result.add(f.get());
}
executor.shutdown();
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的运行方式:
@Test
public void process() throws Exception {
Callable<String> callable = () -> "Do something on ";
ExecuteAlerterTask<String> executeAlerterTask = new ExecuteAlerterTask<>();
List<String> result = executeAlerterTask.process("TaskName", callable);
result.forEach(System.out::println);
}
Run Code Online (Sandbox Code Playgroud)
这是我的问题:如何编写我的Callable使其在一行接受参数i:
Future<T> future = executor.submit(task);
Run Code Online (Sandbox Code Playgroud)
例如,期望的结果将是:
Do something on 1
Do something on 3
Do something on 7
Do something on 2
<...etc...>
Run Code Online (Sandbox Code Playgroud)
如果我的代码有其他问题-请让我知道。
编辑
已移除的工具Callable
上面的代码是我真正想做的事情的抽象:
编辑2
毕竟建议我有以下解决方案:
public class ExecuteAlerterTask<T> {
public List<T> process(String executorName, Collection<Callable<T>> task) throws ExecutionException, InterruptedException {
final ThreadFactory threadFactory = new ThreadFactoryBuilder()
.setNameFormat(executorName + "-%d")
.setDaemon(true)
.build();
ExecutorService executor = Executors.newFixedThreadPool(10, threadFactory);
Collection<Future<T>> futures = executor.invokeAll(task);
List<T> result = new ArrayList<>();
for (Future<T> f : futures) {
result.add(f.get());
}
executor.shutdown();
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
以及运行方式:
@Test
public void process() throws Exception {
Collection<Callable<String>> tasks = new ArrayList<>();
IntStream.range(1, 10).forEach(i -> {
tasks.add(new Task(i).callable);
});
ExecuteAlerterTask<String> executeAlerterTask = new ExecuteAlerterTask<>();
List<String> result = executeAlerterTask.process("TaskName", tasks);
result.forEach(System.out::println);
}
private class Task {
private int i;
private Callable<String> callable = () -> "Doing something on i: " + i;
private Task(int i) {
this.i = i;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑3
运行它的更简单方法:
@Test
public void process() throws Exception {
Collection<Callable<String>> tasks = new ArrayList<>();
IntStream.range(1, 10).forEach(i -> {
tasks.add(() -> "Do something on i: " + i * 2);
});
ExecuteAlerterTask<String> executeAlerterTask = new ExecuteAlerterTask<>();
List<String> result = executeAlerterTask.process("TaskName", tasks);
result.forEach(System.out::println);
}
Run Code Online (Sandbox Code Playgroud)
我想我对最终解决方案很满意。谢谢大家!
这有点不可能。如果变量是 lambda,则不能将变量传递给可调用变量。另一方面,您可以使用自己的特定对象来实现Callable变量并具有变量的设置器:
public class CallableWithParam implements Callable<String> {
// protected for subclassing call()
// volatile for multi-threaded reasons
protected volatile int param = 0;
public void setParam(int param) {
this.param = param;
}
@Override
public String call() {
return "my param is: " + param;
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
@Test
public void process() throws Exception {
CallableWithParam callable = new CallableWithParam() {
@Override
public String call() {
// an anonymous inner class is almost a lambda ;)
return "my param is: " + param + "in subclass";
}
};
callable.setParam(3);
ExecuteAlerterTask<String> executeAlerterTask = new ExecuteAlerterTask<>();
List<String> result = executeAlerterTask.process("TaskName", callable);
result.forEach(System.out::println);
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以在构造函数而不是设置器中设置参数。