Created
December 28, 2017 17:13
-
-
Save chathurangat/8ffc10326aa04d1b20cb4e148ca74e47 to your computer and use it in GitHub Desktop.
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
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.*; | |
public class Application { | |
public static void main(String[] args) throws InterruptedException { | |
System.out.println("main thread is started"); | |
ExecutorService executorService = Executors.newFixedThreadPool(5); | |
CountDownLatch countDownLatch = new CountDownLatch(10); | |
List<Callable<String>> allCallableList = new ArrayList<>(); | |
for (int taskIndex = 0; taskIndex < 10; taskIndex++) { | |
//creating the callable object | |
Callable<String> callable = () -> { | |
System.out.println("Thread [" + Thread.currentThread().getName() + "] is executing the task"); | |
try { | |
Thread.sleep(1000); | |
} catch (InterruptedException e) { | |
System.out.println("sleeping thread get interrupted"); | |
} | |
countDownLatch.countDown(); | |
return "done"; | |
}; | |
allCallableList.add(callable); | |
} | |
//execute all the callables in background threads | |
executorService.invokeAll(allCallableList); | |
countDownLatch.await(); | |
System.out.println("main thread completed "); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment