Newsletter:

(Article) The Executor Framework

Article : The Executor Framework

It's great. It allows you to handle concurrency in Java (or Groovy) without directly using the Thread class.

I first heard about the executor framework in Effective Java (Second Edition) (Item 68: Prefer executors and tasks to threads) and became really interested in it and decided to investigate further.

The Executors class (new in Java 5) provides a bunch of static factory methods for creating executors. With an executor, you can submit a task (Runnable or Callable) that will be performed by that executor. Here's an example:

ExecutorService executor =
Executors.newSingleThreadExecutor();
executor.execute(runnable);
executor.shutdown();

As you can probably guess, this will create an executor which has a single thread. That thread will take the runnable and call its run() method. If the execute method is called multiple times, each runnable that's passed in to the method will be placed on a queue. When the executor's thread is available, it'll pull the first runnable off the queue and run it. When that one finishes, it'll grab the next one, and so on.

[Read More..]

Courtesy:- Insidethemachine.wordpress.com