Why executor framework is better
Thread Pool frees application developer from thread management stuff and allows to focus on business logic. Python Javascript Linux Cheat sheet Contact.
What are the advantages of using an ExecutorService? From JCiP, Section 6. It provides a standard means of decoupling task submission from task execution , describing tasks as Runnable. The Executor implementations also provide lifecycle support and hooks for adding statistics gathering, application management, and monitoring.
Using an Executor is usually the easiest path to implementing a producer-consumer design in your application. Also, despite the boilerplate look and feel, the Oracle API page summarizing the concurrency utilities includes some really solid arguments for using them, not least: Developers are likely to already understand the standard library classes, so there is no need to learn the API and behavior of ad-hoc concurrent components.
For running a single thread, I don't see any clear advantage of using ExecutorService. Poor Resource Management i. It keep on creating new resource for every request. No limit to creating resource. Using Executor framework we can reuse the existing resources and put limit on creating resources.
Unlike the fixed thread pool, the number of threads of this executor pool is not bounded. If all the threads are busy executing some tasks and a new task comes, the pool will create and add a new thread to the executor. As soon as one of the threads becomes free, it will take up the execution of the new tasks. If a thread remains idle for sixty seconds, they are terminated and removed from cache. However, if not managed correctly, or the tasks are not short-lived, the thread pool will have lots of live threads.
This may lead to resource thrashing and hence performance drop. This executor is used when we have a task that needs to be run at regular intervals or if we wish to delay a certain task.
The main difference between the two methods is their interpretation of the delay between consecutive executions of a scheduled job.
The result of the task submitted for execution to an executor can be accessed using the java. Future object returned by the executor. Future can be thought of as a promise made to the caller by the executor. Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
A task submitted to the executor, like above, is asynchronous i. Instead, whenever the task execution is completed, it is set in this Future object by the executor.
The caller can continue executing the main program and when the result of the submitted task is needed he can call. If the task is complete the result is immediately returned to the caller or else the caller is blocked until the execution of this is completed by the executor and the result is computed.
If the caller cannot afford to wait indefinitely before retrieving the result, this wait can be timed as well. This is achieved by the Future. The caller can handle this exception and continue with the further execution of the program.
If there is an exception when executing the task, the call to get method will throw an ExecutionException. Skip to content. Change Language. Related Articles.
Table of Contents. Save Article. Improve Article. Like Article. Last Updated : 14 Aug, ExecutorService executorService. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment?
0コメント