Spring Book – Chapter 19 – Tasks and Scheduling

Atomic Variables

The java.util.concurrent.atomic package defines classes that support atomic operations on single variables. All classes have get and set methods that work like reads and writes on volatile variables. Atomic variables have features that minimize synchronization and help avoid memory consistency errors.

Concurrent Random Numbers

The java.util.concurrent includes a convenience class, ThreadLocalRandom, for applications that expect to use random numbers from multiple threads. For concurrent access, using ThreadLocalRandom instead of Math.random() results in less contention and, ultimately, better performance. Listing 20-8 shows creating random numbers using ThreadLocalRandom.

Listing 20-8. Creating random numbers using ThreadLocalRandom

Other Important Interfaces and Classes

Although we have covered most of the important interfaces and classes in previous sections, there are a few more that need to be mentioned, which are described in the following sections.

Callable Interface

The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception.

The Callable interface offers a call() method, which can return an Object or, more specifically, any type that is introduced in the generalized form as shown in the Listing 20-9 below. The call() method, Computes a result, or throws an exception if unable to do so. To implement Callable with no return value, use Callable<void>.Callable is mainly intended to use with ExecutorService, which is a higher level service than the direct manipulation of Thread. Both Runnable and Callable can be submitted to ExecutorService for execution.

Listing 20-9. Callable interface signature

Future Interface

When you submit a Callable to one of the thread pools, you are provided an instance of Future that is typed to the Callable you passed in. A Future represents the result of an asynchronous computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation. If you would like to use a Future for the sake of cancelling but not provide a usable result, you can declare types of the form Future<?> and return null as a result of the underlying task.

FutureTask Class

The FutureTask class is an implementation of Future that implements Runnable, and so may be executed by an Executor. It’s a cancellable asynchronous computation. This class provides a base implementation of Future, with methods to start and cancel a computation, query to see if the computation is complete, and retrieve the result of the computation. The result can only be retrieved when the computation has completed; the get method will block if the computation has not yet completed. Once the computation has completed, the computation cannot be restarted or cancelled.

Task Scheduling Support by Spring

Now we are in the business side of this chapter. We will see in this section how Spring helps you in doing task scheduling and the levels of abstraction it provides on top of the excellent support provided by Java.

With the help of various interfaces, Spring Framework provides abstractions for asynchronous execution and scheduling of tasks. Spring also provides implementations for these interfaces which provide thread pools or delegate it to Foo-CommonJ (JSR 237 implementation) within an application server environment.

Spring also has appropriate integration classes which can be used to integrate with scheduling support provided by JDK and with Quartz Scheduler.

Spring’s TaskExecutor

You would have now figured out why I spend so much time explaining the Java classes and interfaces. Executor is as pat of Java 5 and it is a name given to the concept of thread pool. The name “executor” is given to it because there is no guarantee that the underlying implementation being a thread pool, single threaded or synchronous in nature. Spring actually abstract away all these implementation details introduced as part of various Java releases away from he developer.

The interface org.springframework.core.task.TaskExecutor is identical to the java.util.concurent.Executor interface in Java. The interface has a single method namely “execute” and the interface signature is as shown in Listing 20-10 below.

Listing 20-10. TaskExecutor interface signature

If you would like to use thread pooling abstraction for your beans, you can always use this interface in your application. There are various implementation provides by Spring built-in, which are very useful according to various requirements in your application. Next section will cover the various types of TaskExecutor in detail. Listing 20-11 shows a sample usage of TaskExecutor. In the bean declaration in XML configuration, implementation class has been left blank as we will see the implementation of TaskExecutor in the next section.

Listing 20-11. A Sample usage of TaskExecutor

Sping bean configuration:

Page Visitors: 23869

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 *