Java Reference
In-Depth Information
Create a smart queue that understands how the tasks work together and knows when to give which task to a
worker thread. This should be considered a last resort as this smart queue will be tightly bound to the application
and may become a maintenance nightmare.
Pattern Variants
Thread pool is a variant in which there is not just one instance of the worker thread, but several instances in a
pool. (Hence the name thread pool.) This pool manages the WorkerThread class instances. The pool creates the
worker threads, determines when they are not needed for a while, and might ultimately destroy some worker
thread instances.
The pool decides how many workers to create at startup and what the maximum will be. The pool can either
choose to create some threads when it starts, so that it always has some threads available, or it can wait until the
first request is made (lazy instantiation).
When there are too many tasks for the current number of threads, however, the system (like a drain) gets clogged.
Several solutions exist:
Increase the number of workers - This works for a limited time only; as this fixes the symptom, not the problem.
Generally, you should choose a better solution.
Don't limit the number of tasks in the queue - Just let the queue grow until the system runs out of memory. This
solution is better than increasing the number of workers, but still will fail due to a shortage of resources.
Limit the size of the queue - When the backlog gets too big, clients no longer make calls to add tasks to the
queue. The queue can then focus on processing the backlog of tasks.
Ask clients to stop sending tasks - Clients can then choose to send either no requests, or fewer requests.
Drop requests that are stored in the queue - If the pool can be certain that the client will retry, it's safe to drop
new requests. Dropping old requests is the right choice when it's likely that the clients posting the request have
gone away.
Let the client run the task itself - The client becomes single-threaded while running the task, and can't create
new tasks until the first task is completed.
Related Patterns
None.
Example
Note:
For a full working example of this code example, with additional supporting classes and/or a RunPattern class,
see “ Worker Thread ” on page 517 of the “ Full Code Examples ” appendix.
In a typical application, certain jobs have to be done. It's not always important that they happen now, just that
they do happen. You can compare this to cleaning a house. It's not important that it happen at a particular time, as
long as somebody does it sometime this week—or month, or year, depending on your standards.
This example uses a Queue to hold tasks. The Queue interface defines two basic methods, put and take . These
methods are used to add and remove tasks, represented by the RunnableTask interface, on the Queue .
Example 4.10 Queue.java
1. public interface Queue{
2. void put(RunnableTask r);
3. RunnableTask take();
4. }
Example 4.11 RunnableTask.java
1. public interface RunnableTask{
 
Search WWH ::




Custom Search