Java ExecutorCompletionService

Java ExecutorCompletionService

In this post, we will see about Java ExecutorCompletionService example.

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:

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

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 named "".

Step 2: Create a class named "FutureTaskMain". This will be our main class.

Let’s run above program to check the output:

Started taskName: Task 1
Started taskName: Task 3
Started taskName: Task 2
Started taskName: Task 4
Completed taskName: Task 4
Result: 3000
Completed taskName: Task 1
Result: 200
Completed taskName: Task 3
Result: 2000
Completed taskName: Task 2
Result: 1200

As you can see with the output, All four tasks started simuntaneously but We retrieved the results based on the completion order with the help of executorCompletionService.take().get() rather than calling get() method on future object.
That’s all about Java ExecutorCompletionService example.


You may also like:

Related Posts

  • Get Thread Id in Java
    30 September

    Get Thread Id in Java

    In this article, we will learn to get thread id of a running thread in Java. An Id is a unique positive number generated at the time of thread creation. This id remains unchanged during the lifetime of the thread. When a thread gets terminated its id can be used to refer another thread, but […]

  • ArrayBlockingQueue in java
    19 September

    ArrayBlockingQueue in java

    In this article, we will understand the Java concurrent queue, BlockingQueue. We will then go deep into it’s one of the implementation, ArrayBlockingQueue. What is BlockingQueue BlockingQueue interface was introduced in Java 5 under concurrent APIs and it represents a thread-safe queue in which elements can be added to and removed from. We can have […]

  • Delay java program by few seconds
    11 December

    Delay Java program for few secs

    In this post, we will see how to delay java program for few secs. We need to delay a java programs in many situation where we want to wait for some other task to finish. There are multiple ways to delay execution of java program. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go […]

  • 29 May

    How to print even and odd numbers using threads in java

    In this post, we will see how to print even and odd numbers using threads in java. see also: How to print sequence using 3 threads in java Problem You are given two threads. You need to print odd numbers using one thread and even numbers using another thread.You need to print in natural order […]

  • wait(),notify() and notifyAll() in java
    22 October

    Why wait(), notify() And notifyAll() methods are in Object Class

    In this post, we will see why wait(), notify() And notifyAll() methods are in Object Class And Not in Thread Class. This is one of the most asked java multithreading interview questions. You might know that wait(), notify() And notifyAll() Methods are in Object class and do you know the reason for the same? 💻 […]

  • Custom BlockingQueue in java
    16 October

    Custom BlockingQueue implementation in java

    In this post, we will see how to create your own custom BlockingQueue. This is one of the most asked java interview questions. You need to implement your own BlockingQueue. This question helps interviewer to get your understanding of multithreading concepts. Here is simple implementation of BlockingQueue. 💻 Awesome Tech Resources: Looking for ⚒️ tech […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.