Skip to content

Instantly share code, notes, and snippets.

@AnjaliManhas
Created July 30, 2020 18:15
Show Gist options
  • Save AnjaliManhas/253136803c083d933e92799436e90859 to your computer and use it in GitHub Desktop.
Save AnjaliManhas/253136803c083d933e92799436e90859 to your computer and use it in GitHub Desktop.
using invokeAll() function
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";
}
});
java.util.List<Future<String>> futures = executorService.invokeAll(callables);
for(Future<String> future : futures){
System.out.println("future.get = " + future.get());
}
executorService.shutdown();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment