执行者服务invokeAll

时间:2016-02-09 05:04:58

标签: java multithreading executorservice

我对可调用接口相当新。我有一些代码,我目前无法编译,只需要一些帮助,为什么......

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);

方法add(Callable)在Collection&gt;类型中不适用于参数(PingTask)StoreReplicationSynchtimeManagerImpl.java

不知道我需要做什么来调用invokeAll。希望得到一些帮助。

感谢

3 个答案:

答案 0 :(得分:1)

错误不在该行。 它在:

pingTasks.add(new PingTask(nonReplicatingRegister.getRegisterName()));

您的集合是Callable,其中PingTask类实现Callable。将集合更改为:

Collection<Callable<String>>

答案 1 :(得分:0)

这是你的错误:

Collection<Callable<PingTask>> pingTasks = new ArrayList<Callable<PingTask>>(nonReplicatingRegisters.size());

PingTask实施Callable<String>,而非Callable<PingTask>。您需要将列表声明为Collection<PingTask>Collection<Callable<String>>

答案 2 :(得分:0)

存在类型不匹配。

 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>类型添加

将导致编译时错误