Examples of MyExecutor


Examples of com.packtpub.java7.concurrency.chapter7.recipe01.executor.MyExecutor

  public static void main(String[] args) {

    /*
     * Creation of the custom executor
     */
    MyExecutor myExecutor=new MyExecutor(2, 4, 1000, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<Runnable>());
   
    /*
     * Create a list to store the objects to control the execution of the tasks
     */
    List<Future<String>> results=new ArrayList<>();
   
    /*
     * Create and submit to the executor 10 tasks
     */
    for (int i=0; i<10; i++) {
      SleepTwoSecondsTask task=new SleepTwoSecondsTask();
      Future<String> result=myExecutor.submit(task);
      results.add(result);
    }
   
    /*
     * Get the result of the execution of the first five tasks
     */
    for (int i=0; i<5; i++){
      try {
        String result=results.get(i).get();
        System.out.printf("Main: Result for Task %d : %s\n",i,result);
      } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
      }
    }
   
    /*
     * Call the shutdown method
     */
    myExecutor.shutdown();
   
    /*
     * Get the results of the execution of the last five tasks
     */
    for (int i=5; i<10; i++){
      try {
        String result=results.get(i).get();
        System.out.printf("Main: Result for Task %d : %s\n",i,result);
      } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
      }
    }
   
    /*
     * Wait for the finalization of the Executor
     */
    try {
      myExecutor.awaitTermination(1, TimeUnit.DAYS);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
   
    /*
 
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.