In order to prevent overhead of thread creation and reuse existed threads Java supports thread pools since version 1.5. The mechanism of thread pool is based on the equally called pattern. Roughly speaking, thread pool is a queue of initialized threads which makes its usage less expensive neither than using classic single Thread approach.
Java Virtual Machine provides fast access to the queue and every thread has its own instruction to be performed; instructions are customized with objects that implement Callable or Runnable.
Java Virtual Machine provides fast access to the queue and every thread has its own instruction to be performed; instructions are customized with objects that implement Callable or Runnable.
Instead of creating threads directly with
operations like Thread th = new Thread(); you may use instance of Executor interface.
Executor executor = new Executor() { public void execute(Runnable command) { command.run(); } }; executor.execute(someRunnableInstance);
Executor uses already existed thread and
makes the thread to perform operations defined at someRunnableInstance.
ExecutorService (http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html)
extends Executor interface within new very useful methods. If you’d like to
have custom class which implements ExecutorService you must override about 13
methods (jdk 1.8.0.31)
Let’s take a look on a few of them
- Future<?> submit(Runnable task), <T> Future<T> submit(Callable<T> task) – submits task to be executed.
- void shutdown() – initiates the shutdown of the submitted task (no guarantee that the task will be stopped)


