...
Thread pools allow a system to limit the maximum number of simultaneous request requests that it processes to a number that it can comfortably serve, rather than terminating all service services when presented with a deluge of requests. Thread pools overcome these issues by controlling the maximum number of worker threads that will can execute concurrently. Each object that supports thread pools accepts a Runnable
or Callable<T>
task and stores it in a temporary queue until resources become available. Additionally, thread life-cycle management overhead is minimized because the threads in a thread pool can be reused and can be efficiently added to or removed from the pool.
Programs that use multiple threads to serve requests should — and security-sensitive programs must — ensure graceful degradation of service during traffic bursts. Use of thread pools is one acceptable approach to meeting this requirement.
Noncompliant Code Example (Thread-Per-Message)
This noncompliant code example demonstrates the Thread-Per-Message design pattern. The RequestHandler
class provides a public static factory method so that callers can obtain its instance. The handleRequest()
method is subsequently invoked to handle each request in its own thread.
...
The Thread-Per-Message strategy fails to provide graceful degradation of service. As threads are created, processing continues normally until some scarce resource is exhausted. For example, a system may allow only a limited number of open file descriptors, even though additional threads can be created to serve requests. When the scarce resource is memory, the system may fail abruptly, resulting in a denial of service.
Compliant Solution (thread pool)
Wiki Markup |
---|
This compliant solution uses a fixed- thread pool that places ana upperstrict boundlimit on the number of concurrently executing threads. Tasks submitted to the pool are stored in an internal queue. This prevents the system from being overwhelmed when attempting to respond to all incoming requests and allows it to degrade gracefully by serving a fixed maximum number of simultaneous clients \[[Tutorials 2008|AA. Bibliography#Tutorials 08]\]. |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="e3e16553c0648c48-f0d507b4-4a864ac3-a1b49f56-0710f41a360d0e725a33a5e8"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | [Interface Executor | http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Executor.html] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3774cc21cd055f11-3e462954-440a4120-938b841c-8fdbe8f2c18319bbf4923f83"><ac:plain-text-body><![CDATA[ | [[Lea 2000 | AA. Bibliography#Lea 00]] | Section 4.1.3 Thread-Per-Message and 4.1.4 Worker Threads | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="6822a315b375ef7a-39c1b599-4355473c-860cafea-8cee00fb62817d3285f8193b"><ac:plain-text-body><![CDATA[ | [[Tutorials 2008 | AA. Bibliography#Tutorials 08]] | [Thread Pools | http://java.sun.com/docs/books/tutorial/essential/concurrency/pools.html] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a6684c50b5a93115-c5001b73-4aaf4f37-8627b519-7ba3299d88095e7924c427a5"><ac:plain-text-body><![CDATA[ | [[Goetz 2006 | AA. Bibliography#Goetz 06]] | Chapter 8, Applying Thread Pools | ]]></ac:plain-text-body></ac:structured-macro> |
...