Friday, July 3, 2020

Executor Framework in java

In this blog, we will tell you about the executor framework introduced in java since JDK 5 and how can we use it in our application development.

Executor framework introduced in version JDK 5 to perform concurrent execution in java and all the interfaces of this framework available in java.util.concurrent package.
Interfaces available in Executor framework given below:
i) Executor
ii) Executor Service
iii) Scheduled Executor Service


All these interfaces represent an asynchronous execution mechanism that is capable of executing multiple tasks concurrently in the background.

Executors is a factory class which is used to provide the instances of these interfaces such as for Executor Service class we need a pool of threads with size 10 so we can use Executors.new Fixed Thread Pool( size of thread pool ) method.

Creating an Executor Service:


ExecutorService executorService = Executors.newFixedThreadPool(10);


ExecutorService implementation class contains many methods but here we see an example of execute(Runnable) and submit(Callable) method, which contains runnable implementation class instance to execute in a concurrent way.


executorService.execute(new Runnable() {
 public void run(){
   System.out.println("Concurrent task");
          }
});


Callable is an interface just like Runnable but the difference is that Callable interface's abstract method call() can return Object type instance. While calling submit(Callable) method, it is returned Future object which contains the return value we can use get() of Future to get a return value.

Future future = executorService.submit(new Callable(){
    public Object call() throws Exception {
        System.out.println("Concurrent Callable");
        return "Callable Result";
    }
});

System.out.println("future.get() = " + future.get());


Now here we need to take one more action shutdown() method to shut down the executor service after all task completion but we have another method also to shutdown executor service such as shutdownNow() for immediate shutdown and await Termination() method will block the thread calling it until either the Executor Service has shut down completely and it is called after shutdown() and shutdown Now().

Conclusion: I hope you like this blog and I hope it is very useful for you in the future whenever you want to save your time to perform additional tasks in a concurrent way.
Thank you and Happy coding.



We are an OTT App Development Company that give start to finish OTT administrations to fabricate endeavor grade programming applications with adaptability of the OTT administrations. Our improvement group is gifted at utilizing AWS stage to manufacture, send, scale, and oversee cloud-based applications with custom highlights. We are likewise knowledgeable about flawlessly relocating your application information from its current tech stack to the OTT administrations. We have effectively finished a few full-scale cloud ventures for new companies, SMEs, and huge scope endeavors. 

Our Other Services 



No comments:

Post a Comment