Created
July 30, 2020 18:15
-
-
Save AnjaliManhas/253136803c083d933e92799436e90859 to your computer and use it in GitHub Desktop.
using invokeAll() function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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