执行器服务 invokeAll
Executor Service invokeAll
我对可调用接口还很陌生。我有一些代码目前无法编译,只是需要一些帮助来解释为什么....
public List<String> getNonPingableRegisters (Collection<RegisterReplicationSynchTime> nonReplicatingRegisters) throws IOException {
int nThreads = 15;
final ExecutorService es = Executors.newFixedThreadPool(nThreads);
Collection<Callable<PingTask>> pingTasks = new ArrayList<Callable<PingTask>>(nonReplicatingRegisters.size());
for (RegisterReplicationSynchTime nonReplicatingRegister : nonReplicatingRegisters) {
pingTasks.add(new PingTask(nonReplicatingRegister.getRegisterName()));
}
List<Future<String>> taskResults = es.invokeAll(pingTasks);
List<String> results = new ArrayList<String>();
for (Future<String> taskResult : taskResults) {
try {
String output = taskResult.get();
if (StringUtils.isNotEmpty(output) ) {
results.add(output);
}
} catch (InterruptedException e) {
// handle accordingly
} catch (ExecutionException e) {
// handle accordingly
}
}
return results;
}
PingTask 在哪里...
public class PingTask implements Callable<String> {
private String hostname = null;
public PingTask(String hostname) {
this.hostname = hostname;
}
public String call() {
Socket socket = null;
boolean reachable = false;
try {
socket = new Socket();
socket.connect(new InetSocketAddress(hostname, 139), 1000); //1 sec timeout
reachable = true;
socket.close();
} catch (IOException e) {
}
finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}
return (reachable ? "" : hostname);
}
}
编译错误在...
List<Future<String>> taskResults = es.invokeAll(pingTasks);
类型 Collection> 中的方法 add(Callable) 不适用于参数 (PingTask) StoreReplicationSynchtimeManagerImpl.java
不确定我需要在这里做什么来调用 invokeAll。非常感谢您的帮助。
谢谢
这是你的错误:
Collection<Callable<PingTask>> pingTasks = new ArrayList<Callable<PingTask>>(nonReplicatingRegisters.size());
PingTask
实现 Callable<String>
,而不是 Callable<PingTask>
。您需要将列表声明为 Collection<PingTask>
或 Collection<Callable<String>>
.
错误不在该行。
它位于:
pingTasks.add(new PingTask(nonReplicatingRegister.getRegisterName()));
您的集合是 Callable,而您的 PingTask class 实现了 Callable。将集合更改为:
Collection<Callable<String>>
类型不匹配。
Collection<Callable<PingTask>> pingTasks = new ArrayList<Callable<PingTask>>
但是 PingTask 声明为
public class PingTask implements Callable<String>
将集合更改为 Collection<PingTask>
pingTasks.add(new PingTask(nonReplicatingRegister.getRegisterName()));
会由于Callable<String>
类型添加而导致编译时错误
我对可调用接口还很陌生。我有一些代码目前无法编译,只是需要一些帮助来解释为什么....
public List<String> getNonPingableRegisters (Collection<RegisterReplicationSynchTime> nonReplicatingRegisters) throws IOException {
int nThreads = 15;
final ExecutorService es = Executors.newFixedThreadPool(nThreads);
Collection<Callable<PingTask>> pingTasks = new ArrayList<Callable<PingTask>>(nonReplicatingRegisters.size());
for (RegisterReplicationSynchTime nonReplicatingRegister : nonReplicatingRegisters) {
pingTasks.add(new PingTask(nonReplicatingRegister.getRegisterName()));
}
List<Future<String>> taskResults = es.invokeAll(pingTasks);
List<String> results = new ArrayList<String>();
for (Future<String> taskResult : taskResults) {
try {
String output = taskResult.get();
if (StringUtils.isNotEmpty(output) ) {
results.add(output);
}
} catch (InterruptedException e) {
// handle accordingly
} catch (ExecutionException e) {
// handle accordingly
}
}
return results;
}
PingTask 在哪里...
public class PingTask implements Callable<String> {
private String hostname = null;
public PingTask(String hostname) {
this.hostname = hostname;
}
public String call() {
Socket socket = null;
boolean reachable = false;
try {
socket = new Socket();
socket.connect(new InetSocketAddress(hostname, 139), 1000); //1 sec timeout
reachable = true;
socket.close();
} catch (IOException e) {
}
finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}
return (reachable ? "" : hostname);
}
}
编译错误在...
List<Future<String>> taskResults = es.invokeAll(pingTasks);
类型 Collection> 中的方法 add(Callable) 不适用于参数 (PingTask) StoreReplicationSynchtimeManagerImpl.java
不确定我需要在这里做什么来调用 invokeAll。非常感谢您的帮助。
谢谢
这是你的错误:
Collection<Callable<PingTask>> pingTasks = new ArrayList<Callable<PingTask>>(nonReplicatingRegisters.size());
PingTask
实现 Callable<String>
,而不是 Callable<PingTask>
。您需要将列表声明为 Collection<PingTask>
或 Collection<Callable<String>>
.
错误不在该行。 它位于:
pingTasks.add(new PingTask(nonReplicatingRegister.getRegisterName()));
您的集合是 Callable,而您的 PingTask class 实现了 Callable。将集合更改为:
Collection<Callable<String>>
类型不匹配。
Collection<Callable<PingTask>> pingTasks = new ArrayList<Callable<PingTask>>
但是 PingTask 声明为
public class PingTask implements Callable<String>
将集合更改为 Collection<PingTask>
pingTasks.add(new PingTask(nonReplicatingRegister.getRegisterName()));
会由于Callable<String>
类型添加而导致编译时错误