我正在尝试使用泛型(我第一次尝试使用泛型)和使用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;
}
}
以下是我运行它的方法:
@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);
}
这是我的问题: 如何编写我的Callable,它会在一行接受参数 i :
Future<T> future = executor.submit(task);
E.g。所需的结果将是:
Do something on 1
Do something on 3
Do something on 7
Do something on 2
<...etc...>
如果我的代码出现其他问题 - 请告诉我。
修改
删除了Callable
以上代码是抽象我真正想做的事情:
EDIT2
完成所有建议之后,我有以下解决方案:
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;
}
}
运行方式:
@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;
}
}
EDIT3
更简单的运行方式:
@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);
}
我想我对最终解决方案非常满意。谢谢大家!
答案 0 :(得分:1)
首先,您根本不需要call()
,也不需要在此类中实现Callable<T>
,因为您从未使用过它。
要以你想要的方式创建一个可调用的,你可以做
Callable<String> task; // from the parameter
int i; // from the loop
Callable<String> wrapper = () -> { return task.call() + " on " + i; }
executor.submit(wrapper);
你实际上是给lambda外部变量i
作为参数。
答案 1 :(得分:1)
这有点不可能。如果那是一个lambda,你就不能将变量传递给callable。另一方面,您可以使用自己的特定对象来实现Callable
并具有变量的setter:
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;
}
}
用法:
@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);
}
或者,您可以在构造函数中设置param而不是setter。