Sunday 18 June 2023

Executor Framework (In progress)

 Executor Framework:

Executor framework/service represents asynchronous execution mechanism which is capable of executing tasks concurrently in the background. Using executor service threads can be easily managed and scheduled.


Why executor framework is important and what are its advantages?

1)Executor service mainly separates task creation and submission. Since both are separate so we can easily replace task anytime.

2)With executor service producer and consumer design  can be easily implemented.

3)Its easy to manage and schedule threads using executor service. We don't need to write Thread Manager of our own which could be erroneous. Like to run 2 or more threads we just need to pass runnable to the execute method of the executor-service.


What are advantages of executor service over Thread approach?

1)Poor resource management:  Thread creates new resource on every request and has no limit on resource creation whereas Executor service reuses existing resources and has limit on resource creation.

2)Not robust as there is no limit on thread creation and we may get stack-overflow exception using thread approach.

3)Overhead creation of time: creation time of thread is extra overhead and is repetitive process for every thread as compared to executor service as it make use of its own threadpool.

4)Response time increases by using threadpool as thread creation time is saved.

5)With Executor service we can change execution policy whenever required by just  replacing executor service implementation class. Like if we want to change one thread to multiple thread we can do so by changing executor service implementation.


Various classes/Interfaces present in Executor framework.

Executor (I)--->ExecutorService(I)-->AbstractExecutorService(C)---->ThreadPoolExecutor(C)


Executor (I)-->ExecutorService(I)-->ScheduledExecutorService(I)-->ScheduledThreadPoolExecutor(C)


                                            Executor (I)

                                                    |

                                       ExecutorService(I)

                                                    |

                      _____________________________________

                     |                                                                           |  

   AbstractExecutorService (AC)                                ScheduledExecutorService(I)

                    |

ThreadPoolExecutor  (C)

                    |

ScheduledThreadPoolExecutor (C) implements ScheduledExecutorService

      

          

Executors  :Executors is a factory class used to create instance of the ExecutorService.


Various concrete implementation of Executor Service:

1)ThreadPoolExecutor

2)ScheduledThreadPoolExecutor

3)ForkJoinPool

    

                       






No comments:

Post a Comment