|
9 | 9 | ---
|
10 | 10 |
|
11 | 11 | ## Intent
|
12 |
| -It is often the case that tasks to be executed are short-lived and |
13 |
| -the number of tasks is large. Creating a new thread for each task would make |
14 |
| -the system spend more time creating and destroying the threads than executing |
15 |
| -the actual tasks. Thread Pool solves this problem by reusing existing threads |
16 |
| -and eliminating the latency of creating new threads. |
| 12 | + |
| 13 | +It is often the case that tasks to be executed are short-lived and the number of tasks is large. |
| 14 | +Creating a new thread for each task would make the system spend more time creating and destroying |
| 15 | +the threads than executing the actual tasks. Thread Pool solves this problem by reusing existing |
| 16 | +threads and eliminating the latency of creating new threads. |
17 | 17 |
|
18 | 18 | ## Explanation
|
| 19 | + |
19 | 20 | Real world example
|
20 | 21 |
|
21 |
| -> We have a large number of relatively short tasks at hand. We need to peel huge amounts of potatoes and serve mighty amount of coffee cups. Creating a new thread for each task would be a waste so we establish a thread pool. |
| 22 | +> We have a large number of relatively short tasks at hand. We need to peel huge amounts of potatoes |
| 23 | +> and serve mighty amount of coffee cups. Creating a new thread for each task would be a waste so we |
| 24 | +> establish a thread pool. |
22 | 25 |
|
23 | 26 | In plain words
|
24 | 27 |
|
25 | 28 | > Thread Pool is a concurrency pattern where threads are allocated once and reused between tasks.
|
26 | 29 |
|
27 | 30 | Wikipedia says
|
28 | 31 |
|
29 |
| -> In computer programming, a thread pool is a software design pattern for achieving concurrency of execution in a computer program. Often also called a replicated workers or worker-crew model, a thread pool maintains multiple threads waiting for tasks to be allocated for concurrent execution by the supervising program. By maintaining a pool of threads, the model increases performance and avoids latency in execution due to frequent creation and destruction of threads for short-lived tasks. The number of available threads is tuned to the computing resources available to the program, such as a parallel task queue after completion of execution. |
| 32 | +> In computer programming, a thread pool is a software design pattern for achieving concurrency of |
| 33 | +> execution in a computer program. Often also called a replicated workers or worker-crew model, |
| 34 | +> a thread pool maintains multiple threads waiting for tasks to be allocated for concurrent |
| 35 | +> execution by the supervising program. By maintaining a pool of threads, the model increases |
| 36 | +> performance and avoids latency in execution due to frequent creation and destruction of threads |
| 37 | +> for short-lived tasks. The number of available threads is tuned to the computing resources |
| 38 | +> available to the program, such as a parallel task queue after completion of execution. |
30 | 39 |
|
31 | 40 | **Programmatic Example**
|
32 | 41 |
|
33 |
| -Let's first look at our task hierarchy. We have a base class and then concrete CoffeeMakingTask and PotatoPeelingTask. |
| 42 | +Let's first look at our task hierarchy. We have a base class and then concrete `CoffeeMakingTask` |
| 43 | +and `PotatoPeelingTask`. |
34 | 44 |
|
35 | 45 | ```java
|
36 | 46 | public abstract class Task {
|
@@ -88,8 +98,8 @@ public class PotatoPeelingTask extends Task {
|
88 | 98 | }
|
89 | 99 | ```
|
90 | 100 |
|
91 |
| -Next we present a runnable Worker class that the thread pool will utilize to handle all the potato peeling and coffee |
92 |
| -making. |
| 101 | +Next we present a runnable `Worker` class that the thread pool will utilize to handle all the potato |
| 102 | +peeling and coffee making. |
93 | 103 |
|
94 | 104 | ```java
|
95 | 105 | public class Worker implements Runnable {
|
@@ -156,9 +166,11 @@ Now we are ready to show the full example in action.
|
156 | 166 | ```
|
157 | 167 |
|
158 | 168 | ## Class diagram
|
159 |
| - |
| 169 | + |
| 170 | + |
160 | 171 |
|
161 | 172 | ## Applicability
|
| 173 | + |
162 | 174 | Use the Thread Pool pattern when
|
163 | 175 |
|
164 | 176 | * You have a large number of short-lived tasks to be executed in parallel
|
|
0 commit comments