Square CSS

Saturday, November 17, 2018

Java Multithreading : ExecutorService

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 */







                    Submit() and execute() are the methods of ExecutorService in Java. One ... 

No comments:

Post a Comment

Some Algorithms

Algorithm: Tower of Hanoi

Tower of Hanoi consists of three towers called as pegs with n number of rings. Rings are of different size.  Conditions to be fulfill...

Popular Posts