简体   繁体   中英

Executor Service invokeAll

I am fairly new to the callable interface. I have some code which I can't get to compile at the moment and just need some help on why....

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

Where PingTask is ...

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

    }

The compile error is at ...

List<Future<String>> taskResults = es.invokeAll(pingTasks);

The method add(Callable) in the type Collection> is not applicable for the arguments (PingTask) StoreReplicationSynchtimeManagerImpl.java

Not sure what I need to do here to make the call to invokeAll. Would appreciate some help.

thanks

Error is not at that line. It's at:

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

Your collection is of Callable where as your PingTask class implements Callable. Change the collection to:

Collection<Callable<String>>

Here's your mistake:

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

PingTask implements Callable<String> , not Callable<PingTask> . You need to declare your list as Collection<PingTask> or Collection<Callable<String>> .

There is a type mis-match.

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

But PingTask is declared as

public class PingTask implements Callable<String>

Change collection as Collection<PingTask>

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

will cause compile time error due to Callable<String> type addition

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM