Thursday, October 07, 2004

1.5's Executor

Put JDK 1.5's Executor to Work for You "The new java.util.concurrent package that is part of JDK 1.5 (Tiger) contains a host of concurrency utilities that are designed to make developing concurrent applications easier. The package provides the Executor framework, which is a collection of classes for managing asynchronous task execution. One way to execute a task asynchronously is to simply fire off a new thread:

new Thread(aRunnable).start();

While this approach is convenient, it has a number of serious drawbacks. Thread creation on some platforms is relatively expensive, and starting a new thread for each task could limit throughput. Even if thread creation were free, in a server application, or any application that will likely be executing many asynchronous tasks, there is no way to bind the number of threads created. This situation can cause the application to crash with OutOfMemoryException, or perform poorly because of resource exhaustion. A stable server application needs a policy to govern how many requests it can be processing at once. A thread pool is an ideal way to manage the number of tasks executing concurrently.

The Executor framework simplifies the management of asynchronous tasks and decouples task submission and management from task execution. The various implementations of Executor in java.util.concurrent can support thread-per-task execution, a single background thread for executing tasks, or thread pooling (and it would even be possible to write an Executor that executes the Runnable in another Java Virtual Machine). It can provide for execution policies such as before-execution and after-execution hooks, saturation policies, queuing policies, and so on."

There's a 1.4 backport of the concurrent library available. The CopyOnWriteArrayList and ConcurrentHashMap would be useful for JRDF. A more general article on the new 1.5 features is online as well.

No comments: