Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Wiki Markup
Many programs must address the problem of handling a series of incoming requests. One simple concurrency strategy is the thread-per-message design pattern, which uses a new thread for each request \[[Lea 2000|AA. Bibliography#LeaReferences#Lea 00]\]. This pattern is generally preferred over sequential executions of time-consuming, I/O-bound, session-based, or isolated tasks.

Wiki Markup
However, the pattern also introduces overheads not seen in sequential execution, including the time and resources required for thread creation and scheduling, for task processing, for resource allocation and deallocation, and for frequent context switching \[[Lea 2000|AA. Bibliography#LeaReferences#Lea 00]\]. Furthermore, an attacker can cause a denial of service (DoS) by overwhelming the system with too many requests all at once, causing the system to become unresponsive rather than degrading gracefully. From a safety perspective, one component can exhaust all resources because of an intermittent error, consequently starving all other components.

...

Wiki Markup
This compliant solution uses a fixed thread pool that places a strict limit 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#TutorialsReferences#Tutorials 08]\].

Code Block
bgColor#ccccff
// class Helper remains unchanged

final class RequestHandler {
  private final Helper helper = new Helper();
  private final ServerSocket server;
  private final ExecutorService exec;

  private RequestHandler(int port, int poolSize) throws IOException {
    server = new ServerSocket(port);
    exec = Executors.newFixedThreadPool(poolSize);
  }

  public static RequestHandler newInstance(int poolSize) 
                                           throws IOException {
    return new RequestHandler(0, poolSize);
  }

  public void handleRequest() {
    Future<?> future = exec.submit(new Runnable() {
        @Override public void run() {
          try {
            helper.handle(server.accept());
          } catch (IOException e) {
            // Forward to handler
          }
        }
    });
  }
  // ... other methods such as shutting down the thread pool 
  // and task cancellation ...
}

Wiki Markup
According to the Java API documentation for the {{Executor}} interface \[[API 2006|AA. Bibliography#APIReferences#API 06]\]

Wiki Markup
\[The interface {{Executor}} is\] an object that executes submitted {{Runnable}} tasks. This interface provides a way of decoupling task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc. An {{Executor}} is normally used instead of explicitly creating threads.

...

Wiki Markup
The choice of {{newFixedThreadPool}} is not always appropriate. Refer to the Java API documentation for guidance on choosing among the following methods to meet specific design requirements \[[API 2006|AA. Bibliography#APIReferences#API 06]\]: 

  • newFixedThreadPool()
  • newCachedThreadPool()
  • newSingleThreadExecutor()
  • newScheduledThreadPool()

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="e15bc7fde97a748b-640a0887-40da4797-88968e0b-c6779d8cd8232d514369c554"><ac:plain-text-body><![CDATA[

[[API 2006

AA. Bibliography#API References#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="880bad4a4b0b56fd-44167045-4ee449f3-b313a793-e50670e62c6009499bfc44d5"><ac:plain-text-body><![CDATA[

[[Lea 2000

AA. Bibliography#Lea References#Lea 00]]

4.1.3, Thread-Per-Message; 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="a6b814f88501b1ca-37cda9f5-413a45cf-9652a8f4-05f2ff8a4ab4b2723295c818"><ac:plain-text-body><![CDATA[

[[Tutorials 2008

AA. Bibliography#Tutorials References#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="92aa2c4a0d25544e-6af77c05-4a9a42d9-87b6b895-6fd147717c65a356a8c22e6c"><ac:plain-text-body><![CDATA[

[[Goetz 2006

AA. Bibliography#Goetz References#Goetz 06]]

Chapter 8, Applying Thread Pools

]]></ac:plain-text-body></ac:structured-macro>

...