ExecutorService Interface
The Executor implementations provided in java.util.concurrent package implement ExecutorService, which is a more extensive interface compared to the Executor interface. Listing 20-6 shows the ExecutorService interface. As you can see from the listing, this interface extends from the Executor interface and adds on new methods to make it more extensive.
Listing 20-6. ExecutorService interface source
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package java.util.concurrent; import java.util.List; import java.util.Collection; import java.security.PrivilegedAction; import java.security.PrivilegedExceptionAction; public interface ExecutorService extends Executor { void shutdown(); List<Runnable> shutdownNow(); boolean isShutdown(); boolean isTerminated(); boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException; <T> Future<T> submit(Callable<T> task); <T> Future<T> submit(Runnable task, T result); Future<?> submit(Runnable task); <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)throws InterruptedException; <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,long timeout, TimeUnit unit)throws InterruptedException; <T> T invokeAny(Collection<? extends Callable<T>> tasks)throws InterruptedException, ExecutionException; <T> T invokeAny(Collection<? extends Callable<T>> tasks,long timeout, TimeUnit unit)throws InterruptedException, ExecutionException, TimeoutException; } |
In addition to the “execute” method in Executor interface, it has a much more versatile “submit” method. The “submit” button similar to “execute” method accepts Runnable objects but it also accepts “Callable” objects which allow tasks to return value. In addition to this, it also allows submitting large amount of “Callable” objects which is handy in large-scale applications.
ScheduledExecutorService Interface
ScheduledExecutorService interface adds onto its parent interface “ExecutorService” with method “schedule”, which takes in “Runnable” or “Callable” tasks and executes it after a specified delay. It also has other methods like “scheduleAtFixedRate” and “scheduleWithFixedDelay” which executes specified tasks repeatedly at defined intervals as configured. Listing 20-7 shows the ScheduledExecutorService interface.
Listing 20-7. ScheduledExecutorService interface source
1 2 3 4 5 6 7 8 9 |
package java.util.concurrent; import java.util.concurrent.atomic; import java.util; public interface ScheduledExecutorService extends ExecutorService { public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit); public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit); public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit); public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit); } |
Executors Class
The java.util.concurrent.Executors, class is very important in this concurrency package. This class hosts factory and utility methods for the various classes defined in java.util.concurrent package; Executor, ExecutorService, ScheduledExecutorService, ThreadFactory and Callable.
Executors class supports the following kinds of methods as summarized below:
- Methods which creates and return ExecutorService with commonly useful configuration settings.
- Methods which creates and return ScheduledExecutorService with commonly useful configuration settings.
- Methods which creates and return a “wrapped” ExecutorService disabling reconfiguration by making implementation-specific methods not accessible.
- Methods which creates and return ThreadFactory which sets newly created threads to a known state.
Thread Pools
Most of the executor implementations in java.util.concurrent use thread pools, which consist of worker threads. Using these worker threads reduces the overhead due to thread creation.
One common type of thread pool is the fixed thread pool. This type of pool always has a specified number of threads running and if a thread is terminated due to any reason while it is still in use, it is automatically replaced with a new thread in the pool. A simple way to create an executor which uses a fixed thread pool is to invoke the “newFixedThreadPool” factory method in Executors class.
Fork/Join
Java 7 has introduced the Fork/Join concept of dealing with computing intensive tasks in your enterprise application because these requirements are becoming more and more common. Using this framework, it is easy to distribute the intensive task to several works and then wait for it to complete and produce the result which can be used in a meaningful way.
If you want to use this feature and are not using Java 7 for your application, you can include the jar jsr166y.jar in your application classpath. By doing so, you can use the related classes in JDK 1.5 and 1.6 without having to install the latest JDK.
Concurrent Collections
Java provides classes capable of handling large collection of data without you bothering too much about the synchronization logic. The important collection interfaces provided are:
- BlockingQueue
- BlockingDeque
- ConcurrentMap
- ConcurrentNavigableMap
The following sections will cover these important current data structures in detail.
Page Visitors: 25010

Tomcy John

Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023