Java.util.concurrent Executors: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
No edit summary
Line 16: Line 16:
int threadCount = 10;
int threadCount = 10;
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
...
executor.execute(new Runtime() {
  public void run() {
      ...
  }
});


</pre>
</pre>


The ExecutorService so created has a fixed number of threads operating off a shared unbounded queue. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.
The ExecutorService so created has a fixed number of threads operating off a shared unbounded queue. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.

Revision as of 04:50, 21 January 2016

Internal

Overview

An executor is a mechanism that allows executing tasks on a thread different than the one that submits the task for execution. Executors are preferred to explicitly creating Thread instances. The executor decouples the task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc.

Difference between Executor and ExecutorService.

The package offers an executor instance factory class ava.util.concurrent.Executors.

Typical Usage

int threadCount = 10;
ExecutorService executor = Executors.newFixedThreadPool(threadCount);

...

executor.execute(new Runtime() {
   public void run() {
      ...
   }
});

The ExecutorService so created has a fixed number of threads operating off a shared unbounded queue. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.