Executor Service is an interface in Java to create a pool of threads or worker threads to execute a task asynchronously. The instance of ExecutorService is created and with the help of its instance, a pool of threads are created. The thread pool can have single or multiple threads depending upon the requirement.
Creation of thread pool :
A thread pool can be created with the help of ExecutorService in the below-mentioned way.
- Creation of Single thread by using newSingleThreadExecutor().
- Creation of multiple threads by using newFixedThreadPool(n) where n is the required count of the worker threads.
- Creation of catched thread pool by using newCachedThreadPool().
Syntax is mentioned below :
ExecutorService execute=Executors.newSingleThreadExecutor(); /* For single thread creation */ ExecutorService execute=Executors.newFixedThreadPool(); /* For a fixed count of worker threads */ ExecutorService execute=Executors.newCachedThreadPool(); /* For catched thread pool */ |
No comments:
Post a Comment