Saturday 7 December 2019

Thread Pool-Executor Service


 Implementing Executor Service in Java

   When you want to get the data from the various services and make the service calls concurrently.


    ExecutorService executors = Executors.newFixedThreadPool(2);  //creats the thread pool of size 2

        //Using method reference
   Callable<Map<Integer, String>> service1cal= service1::getData; 

       //Using lamda expression
Callable<List<SomeBean>> service2cal= ()-> {
          service2.getData(input1);
       };


       Future<Map<Integer, String>> service1f= executors.submit(service1cal);
       Future<List<SomeBean>> service2f= executors.submit(service2cal);
 
       Map<Integer, String>  service1datamap=service1f.get();  //its a blocking statement ,it will make the main thread to wait until its ex
       List<SomeBean> service2list=service2f.get();
       executors.shutdown();