|
23 | 23 |
|
24 | 24 | package org.queue.load.leveling;
|
25 | 25 |
|
| 26 | +import java.util.concurrent.ExecutorService; |
| 27 | +import java.util.concurrent.Executors; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | + |
26 | 30 | import org.slf4j.Logger;
|
27 | 31 | import org.slf4j.LoggerFactory;
|
28 | 32 |
|
@@ -58,37 +62,56 @@ public class App {
|
58 | 62 |
|
59 | 63 | private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
60 | 64 |
|
| 65 | + //Executor shut down time limit. |
| 66 | + private static final int SHUTDOWN_TIME = 15; |
| 67 | + |
61 | 68 | /**
|
62 | 69 | * Program entry point
|
63 | 70 | *
|
64 | 71 | * @param args command line args
|
65 | 72 | */
|
66 | 73 | public static void main(String[] args) {
|
| 74 | + |
| 75 | + // An Executor that provides methods to manage termination and methods that can |
| 76 | + // produce a Future for tracking progress of one or more asynchronous tasks. |
| 77 | + ExecutorService executor = null; |
| 78 | + |
67 | 79 | try {
|
68 | 80 | // Create a MessageQueue object.
|
69 | 81 | MessageQueue msgQueue = new MessageQueue();
|
70 | 82 |
|
71 |
| - LOGGER.info("All the TaskGenerators started."); |
| 83 | + LOGGER.info("Submitting TaskGenerators and ServiceExecutor threads."); |
72 | 84 |
|
73 | 85 | // Create three TaskGenerator threads. Each of them will submit different number of jobs.
|
74 | 86 | Runnable taskRunnable1 = new TaskGenerator(msgQueue, 5);
|
75 | 87 | Runnable taskRunnable2 = new TaskGenerator(msgQueue, 1);
|
76 | 88 | Runnable taskRunnable3 = new TaskGenerator(msgQueue, 2);
|
77 | 89 |
|
78 |
| - Thread taskGenerator1 = new Thread(taskRunnable1, "Task_Generator_1"); |
79 |
| - Thread taskGenerator2 = new Thread(taskRunnable2, "Task_Generator_2"); |
80 |
| - Thread taskGenerator3 = new Thread(taskRunnable3, "Task_Generator_3"); |
| 90 | + // Create e service which should process the submitted jobs. |
| 91 | + Runnable srvRunnable = new ServiceExecutor(msgQueue); |
| 92 | + |
| 93 | + // Create a ThreadPool of 2 threads and |
| 94 | + // submit all Runnable task for execution to executor.. |
| 95 | + executor = Executors.newFixedThreadPool(2); |
| 96 | + executor.submit(taskRunnable1); |
| 97 | + executor.submit(taskRunnable2); |
| 98 | + executor.submit(taskRunnable3); |
81 | 99 |
|
82 |
| - taskGenerator1.start(); |
83 |
| - taskGenerator2.start(); |
84 |
| - taskGenerator3.start(); |
| 100 | + // submitting serviceExecutor thread to the Executor service. |
| 101 | + executor.submit(srvRunnable); |
85 | 102 |
|
86 |
| - LOGGER.info("Service Executor started."); |
| 103 | + // Initiates an orderly shutdown. |
| 104 | + LOGGER.info("Intiating shutdown. Executor will shutdown only after all the Threads are completed."); |
| 105 | + executor.shutdown(); |
87 | 106 |
|
88 |
| - // First create e service which will process all the jobs. |
89 |
| - Runnable srvRunnable = new ServiceExecutor(msgQueue); |
90 |
| - Thread srvExec = new Thread(srvRunnable, "Service_Executor_Thread"); |
91 |
| - srvExec.start(); |
| 107 | + // Wait for SHUTDOWN_TIME seconds for all the threads to complete |
| 108 | + // their tasks and then shut down the executor and then exit. |
| 109 | + if ( !executor.awaitTermination(SHUTDOWN_TIME, TimeUnit.SECONDS) ) { |
| 110 | + LOGGER.info("Executor was shut down and Exiting."); |
| 111 | + executor.shutdownNow(); |
| 112 | + } |
| 113 | + } catch (InterruptedException ie) { |
| 114 | + LOGGER.error(ie.getMessage()); |
92 | 115 | } catch (Exception e) {
|
93 | 116 | LOGGER.error(e.getMessage());
|
94 | 117 | }
|
|
0 commit comments