Spring Book – Chapter 19 – Tasks and Scheduling

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

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

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: 24422

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *