我试图用generics(我第一次尝试使用generics)和使用exeputorService来实现" 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);
}
这是我的问题:如何写出我的可召唤,它会在一行中接受参数 i :
Future<T> future = executor.submit(task);
例如。所需的结果是:
Do something on 1
Do something on 3
Do something on 7
Do something on 2
<...etc...>
如果我的代码有问题 - 请告诉我。
编辑
删除工具可召唤
上面的代码是抽象我真正想做的事情:
- Intrange确实是我从SQL获取数据的批次集。可召唤真正实现逻辑如何处理这些SQL批次。
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);
}
我想我对最终解决方案感到非常满意。谢谢所有!
首先,您根本不需要call()
,您也不需要在此类中实现Callable<T>
,因为您从不使用它。
要按照您想要的方式创建Callable
,您可以做:
Callable<String> task; // from the parameter
int i; // from the loop
Callable<String> wrapper = () -> { return task.call() + " on " + i; }
executor.submit(wrapper);
您本质上将lambda作为参数提供外部变量i
。
这是不可能的。如果是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;
}
}
用法:
@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。