Follow these steps to implement the example:
- Create a class named Task that implements the Runnable interface:
public class Task implements Runnable {
- Declare a private long attribute named milliseconds:
private final long milliseconds;
- Implement the constructor of the class to initialize its attribute:
public Task (long milliseconds) {
this.milliseconds=milliseconds;
}
- 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\n",
Thread.currentThread().getName());
try {
TimeUnit.MILLISECONDS.sleep(milliseconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.printf("%s: End\n",
Thread.currentThread().getName());
}
- 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 {
- Create a new Executor object using the newCachedThreadPool() method of the Executors class:
ThreadPoolExecutor executor = (ThreadPoolExecutor)
Executors.newCachedThreadPool();
- 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);
}
- 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);
}
- Shut down the executor using the shutdown() method:
executor.shutdown();
- 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);
}
- Wait for the finalization of the executor using the awaitTermination() method:
executor.awaitTermination(1, TimeUnit.DAYS);
- Display a message indicating the end of the program:
System.out.printf("Main: End of the program.\n");
}
- 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\n",
executor.getCorePoolSize());
System.out.printf("Main: Executor: Pool Size: %d\n",
executor.getPoolSize());
System.out.printf("Main: Executor: Active Count: %d\n",
executor.getActiveCount());
System.out.printf("Main: Executor: Task Count: %d\n",
executor.getTaskCount());
System.out.printf("Main: Executor: Completed Task Count: %d\n",
executor.getCompletedTaskCount());
System.out.printf("Main: Executor: Shutdown: %s\n",
executor.isShutdown());
System.out.printf("Main: Executor: Terminating: %s\n",
executor.isTerminating());
System.out.printf("Main: Executor: Terminated: %s\n",
executor.isTerminated());
System.out.printf("*********************\n");
}