Executing Callable tasks using ExecutorService

Category : Java | Sub Category : Java 8 Features | By Runner Dev Last updated: 2020-11-07 12:03:52 Viewed : 219


Executing Callable tasks using ExecutorService

1.   java.util.concurrent.ExecutorService:

An Executor that provides methods to manage termination and methods that can produce a Future for tracking progress of one or more asynchronous tasks.

     2.  Example:

ExecutorService.java 

 

package runnerdev;

 

import java.util.ArrayList;

import java.util.List;

import java.util.concurrent.Callable;

import java.util.concurrent.ExecutionException;

import java.util.concurrent.Executors;

import java.util.concurrent.Future;

 

public class ExecutorService {

 

     public static void main(String[] args) throws InterruptedException, ExecutionException {

           Future<List<Product>> producSertList = getProducts();

           List<Product> productsList = producSertList.get();

           productsList.forEach(product -> {

                System.out.println(product);

           });

     }

 

     static Future<List<Product>> getProducts() {

           java.util.concurrent.ExecutorService executorService = Executors.newFixtedThreadPool(5);

 

           Callable<List<Product>> callable = () -> {

                List<Product> list = new ArrayList<Product>();

                list.add(new Product(111, "Thoshib", 1000.00, 11));

                list.add(new Product(112, "Samsung", 3000.00, 12));

                list.add(new Product(113, "Carbon", 1000.00, 9));

                Thread.sleep(2000);

                return list;

           };

           Future<List<Product>> productList = executorService.submit(callable);

           return productList;

 

     }

}

 

class Product {

     private int prodID;

     private String prodName;

     private Double prodPrice;

     private int prodQnty;


     @Override

     public String toString() {

           return "Product [prodID=" + prodID + ", prodName=" + prodName + ", prodPrice=" + prodPrice + ", prodQnty="

                      + prodQnty + "]";

     }     

 

     public Product(int prodID, String prodName, Double prodPrice, int prodQnty) {

           super();

           this.prodID = prodID;

           this.prodName = prodName;

           this.prodPrice = prodPrice;

           this.prodQnty = prodQnty;

     }

 

     /**

      * @return the prodPrice

      */

     public Double getProdPrice() {

           return prodPrice;

     }

 

     /**

      * @param prodPrice the prodPrice to set

      */

     public void setProdPrice(Double prodPrice) {

           this.prodPrice = prodPrice;

     }

 

     /**

      * @return the prodQnty

      */

     public int getProdQnty() {

           return prodQnty;

     }

 

     /**

      * @param prodQnty the prodQnty to set

      */

     public void setProdQnty(int prodQnty) {

           this.prodQnty = prodQnty;

     }

 

     /**

      * @return the prodID

      */

     public int getProdID() {

           return prodID;

     }

 

     /**

      * @param prodID the prodID to set

      */

     public void setProdID(int prodID) {

           this.prodID = prodID;

     }

 

     /**

      * @return the prodName

      */

     public String getProdName() {

           return prodName;

     }

 

     /**

      * @param prodName the prodName to set

      */

     public void setProdName(String prodName) {

           this.prodName = prodName;

     }

 

} 

 

Output:

Product [prodID=111, prodName=Thoshib, prodPrice=1000.0, prodQnty=11]

Product [prodID=112, prodName=Samsung, prodPrice=3000.0, prodQnty=12]

Product [prodID=113, prodName=Carbon, prodPrice=1000.0, prodQnty=9]

Search
Related Articles

Leave a Comment: