【问题标题】:Executor Service invokeAll执行服务调用全部
【发布时间】:2016-02-09 05:04:58
【问题描述】:

我对可调用接口相当陌生。我有一些代码目前无法编译,只需要一些帮助说明原因....

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。不胜感激。

谢谢

【问题讨论】:

    标签: java multithreading executorservice


    【解决方案1】:

    错误不在该行。 它位于:

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

    您的集合属于 Callable,因为您的 PingTask 类实现了 Callable。将集合更改为:

    Collection<Callable<String>>
    

    【讨论】:

    • 谢谢。这样就完成了。
    【解决方案2】:

    这是你的错误:

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

    PingTask 实现 Callable&lt;String&gt;,而不是 Callable&lt;PingTask&gt;。您需要将您的列表声明为Collection&lt;PingTask&gt;Collection&lt;Callable&lt;String&gt;&gt;

    【讨论】:

    • 就是这样。即使我有 Collection pingTasks = new ArrayList(nonReplicatingRegisters.size());我仍然得到 ExecutorService 类型中的方法 invokeAll(Collection>) 不适用于参数 (Collection) StoreReplicationSynchtimeManagerImpl.java
    • @Richie 所以更新你的帖子,我们再看看。
    • @Richie,我尝试编译(衍生)您的代码,我得到的唯一错误是未处理的 InterruptedException
    【解决方案3】:

    类型不匹配。

     Collection<Callable<PingTask>> pingTasks = new ArrayList<Callable<PingTask>>
    

    但是 PingTask 被声明为

    public class PingTask implements Callable<String>
    

    将集合更改为Collection&lt;PingTask&gt;

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

    由于Callable&lt;String&gt;类型添加会导致编译时错误

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 2023-03-08
      相关资源
      最近更新 更多