JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Showing posts with label ExecutorCompletionService. Show all posts
Showing posts with label ExecutorCompletionService. Show all posts

Java ExecutorCompletionService example

8:25 AM
ExecutorCompletionService class implements CompletionService. This class returns Future object in completion order. Why you may need to use ExecutorCompletionService: Let’s understand with the help of scenario: Let’s say you have 5 tasks, you submit it to the executors and you want to perform some operation as soon as task completes.Let’s also assume that the first task takes the longest time. If you use Executors in this case, when you call future.get() on the first task, get operation will be blocked and even though other tasks may have completed, you won’t be able to proceed further. To solve this issue, you can use ExecutorCompletionService.ExecutorCompletionService returns futures objects based on completion order, so whichever task executes first, will be returned first. You just need to call executorCompletionService .take() to get completed Future object. Java ExecutorCompletionService example: Let’s create a very simple example. Step 1: Create a Callable task

.