Update README.md

This commit is contained in:
Ilkka Seppälä 2020-09-13 18:23:54 +03:00
parent 6cb5b4a683
commit 87093cf221
4 changed files with 23 additions and 73 deletions

View File

@ -9,16 +9,19 @@ tags:
---
## Intent
It is often the case that tasks to be executed are short-lived and
the number of tasks is large. Creating a new thread for each task would make
the system spend more time creating and destroying the threads than executing
the actual tasks. Thread Pool solves this problem by reusing existing threads
and eliminating the latency of creating new threads.
It is often the case that tasks to be executed are short-lived and the number of tasks is large.
Creating a new thread for each task would make the system spend more time creating and destroying
the threads than executing the actual tasks. Thread Pool solves this problem by reusing existing
threads and eliminating the latency of creating new threads.
## Explanation
Real world example
> 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.
> 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.
In plain words
@ -26,11 +29,18 @@ In plain words
Wikipedia says
> 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.
> 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.
**Programmatic Example**
Let's first look at our task hierarchy. We have a base class and then concrete CoffeeMakingTask and PotatoPeelingTask.
Let's first look at our task hierarchy. We have a base class and then concrete `CoffeeMakingTask`
and `PotatoPeelingTask`.
```java
public abstract class Task {
@ -88,8 +98,8 @@ public class PotatoPeelingTask extends Task {
}
```
Next we present a runnable Worker class that the thread pool will utilize to handle all the potato peeling and coffee
making.
Next we present a runnable `Worker` class that the thread pool will utilize to handle all the potato
peeling and coffee making.
```java
public class Worker implements Runnable {
@ -156,9 +166,11 @@ Now we are ready to show the full example in action.
```
## Class diagram
![alt text](./etc/thread-pool.png "Thread Pool")
![alt text](./etc/thread_pool_urm.png "Thread Pool")
## Applicability
Use the Thread Pool pattern when
* You have a large number of short-lived tasks to be executed in parallel

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

View File

@ -1,62 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
<class id="1" language="java" name="com.iluwatar.Task" project="thread-pool"
file="/thread-pool/src/main/java/com/iluwatar/Task.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="343" y="579"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="2" language="java" name="com.iluwatar.Worker" project="thread-pool"
file="/thread-pool/src/main/java/com/iluwatar/Worker.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="344" y="389"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="3" language="java" name="com.iluwatar.CoffeeMakingTask" project="thread-pool"
file="/thread-pool/src/main/java/com/iluwatar/CoffeeMakingTask.java" binary="false" corner="BOTTOM_RIGHT">
<position height="124" width="156" x="194" y="717"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="4" language="java" name="com.iluwatar.PotatoPeelingTask" project="thread-pool"
file="/thread-pool/src/main/java/com/iluwatar/PotatoPeelingTask.java" binary="false" corner="BOTTOM_RIGHT">
<position height="124" width="169" x="390" y="717"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<association id="5">
<end type="SOURCE" refId="2" navigable="false">
<attribute id="6" name="task"/>
<multiplicity id="7" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="1" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<generalization id="8">
<end type="SOURCE" refId="4"/>
<end type="TARGET" refId="1"/>
</generalization>
<generalization id="9">
<end type="SOURCE" refId="3"/>
<end type="TARGET" refId="1"/>
</generalization>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</classifier-display>
<association-display labels="true" multiplicity="true"/>
</class-diagram>

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB