How to do it...

Follow these steps to implement the example:

  1. Create a class named Task that implements the Runnable interface:
        public class Task implements Runnable {
  1. Declare a private long attribute named milliseconds:
        private final long milliseconds;
  1. Implement the constructor of the class to initialize its attribute:
        public Task (long milliseconds) { 
this.milliseconds=milliseconds;
}
  1. Implement the run() method. Put the thread to sleep for the number of milliseconds specified by the milliseconds attribute:
        @Override 
public void run() {

System.out.printf("%s: Begin ",
Thread.currentThread().getName());
try {
TimeUnit.MILLISECONDS.sleep(milliseconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.printf("%s: End ",
Thread.currentThread().getName());

}
  1. Implement the main class of the example by creating a class named Main with a main() method:
        public class Main { 

public static void main(String[] args) throws Exception {
  1. Create a new Executor object using the newCachedThreadPool() method of the Executors class:
        ThreadPoolExecutor executor = (ThreadPoolExecutor)
Executors.newCachedThreadPool();
  1. Create and submit 10 Task objects to the executor. Initialize the objects with a random number:
        Random random=new Random(); 
for (int i=0; i<10; i++) {
Task task=new Task(random.nextInt(10000));
executor.submit(task);
}
  1. Create a loop with five steps. In each step, write information about the executor by calling the showLog() method and putting the thread to sleep for a second:
        for (int i=0; i<5; i++){ 
showLog(executor);
TimeUnit.SECONDS.sleep(1);
}
  1. Shut down the executor using the shutdown() method:
        executor.shutdown();
  1. Create another loop with five steps. In each step, write information about the executor by calling the showLog() method and putting the thread to sleep for a second:
        for (int i=0; i<5; i++){ 
showLog(executor);
TimeUnit.SECONDS.sleep(1);
}
  1. Wait for the finalization of the executor using the awaitTermination() method:
        executor.awaitTermination(1, TimeUnit.DAYS);
  1. Display a message indicating the end of the program:
          System.out.printf("Main: End of the program.
"); 
}
  1. Implement the showLog() method that receives Executor as a parameter. Write information about the size of the pool, the number of tasks, and the status of the executor:
        private static void showLog(ThreadPoolExecutor executor) { 
System.out.printf("*********************");
System.out.printf("Main: Executor Log");
System.out.printf("Main: Executor: Core Pool Size: %d ",
executor.getCorePoolSize());
System.out.printf("Main: Executor: Pool Size: %d ",
executor.getPoolSize());
System.out.printf("Main: Executor: Active Count: %d ",
executor.getActiveCount());
System.out.printf("Main: Executor: Task Count: %d ",
executor.getTaskCount());
          System.out.printf("Main: Executor: Completed Task Count: %d
",
executor.getCompletedTaskCount());
System.out.printf("Main: Executor: Shutdown: %s ",
executor.isShutdown());
System.out.printf("Main: Executor: Terminating: %s ",
executor.isTerminating());
System.out.printf("Main: Executor: Terminated: %s ",
executor.isTerminated());
System.out.printf("********************* ");
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.106.237