Skip to content

Instantly share code, notes, and snippets.

@AnjaliManhas
Created July 30, 2020 17:55
Show Gist options
  • Save AnjaliManhas/ff5acf59e193084ea13646852088b7cb to your computer and use it in GitHub Desktop.
Save AnjaliManhas/ff5acf59e193084ea13646852088b7cb to your computer and use it in GitHub Desktop.
invokeAny() implementation
package com.wordpress.compilationerrors;
import java.lang.Runnable;
import java.lang.*;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.*;
public class ExecutorServiceExample {
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService executorService = Executors.newSingleThreadExecutor();
Set<Callable<String>> callables = new HashSet<Callable<String>>();
callables.add(new Callable<String>() {
public String call() throws Exception {
return "Task 1";
}
});
callables.add(new Callable<String>() {
public String call() throws Exception {
return "Task 2";
}
});
callables.add(new Callable<String>() {
public String call() throws Exception {
return "Task 3";
}
});
String result = executorService.invokeAny(callables);
System.out.println("result = " + result);
executorService.shutdown();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment