Spring Book – Chapter 19 – Tasks and Scheduling


JSR 166 added support for concurrency utilities in the Java platform. The JSR 236’s, also known as Concurrency Utilities for Java EE, goal was to extend that support to the Java EE platform by adding asynchronous abilities to different application components.

You are might be asking “Why are we talking about JSR in a Spring book?” Spring’s scheduler adapter is based on standard JSR-236 API and is the building block of Spring’s abstraction on concurrency.

JSR 237

JCP defines JSR 237 as follows:

A work manager API providing for execution of concurrent work items within managed environments.

– jcp.org

Foo-CommonJ is a JSR 237 Timer and WorkManager implementation. It is designed to be used in containers that do not come with their own implementation – mainly plain servlet containers like Tomcat. It can also be used in fully blown JEE applications servers that do not have a WorkManager API or have a non-standard API like JBoss.

The common use case is that a Servlet or JSP needs to aggregate data from multiple sources and display them in one page. Doing your own threading inside a managed environment like a J2EE container is inappropriate and should never be done. In this case the WorkManager API can be used to retrieve the data in parallel.

Java Concurrency APIs

As a general best practice you shouldn’t use threads directly in your application. Thread management is low-level in nature and should be delegated to the framework to handle it transparently. With Java version 5 and 6 there has been higher-level support from the language itself to address this to a greater level. All the support by the language is inside the java.util.concurrent package.

Java also provides the concept of locks, wherein it allows parts of a program to be accessed by various threads at the same time. Use of keyword “synchronized”, does this for you in transparent manner.

The Java programming language provides two basic synchronization idioms: synchronized methods and synchronized statements. To make a method synchronized, simply add the synchronized keyword to its declaration as shown in Listing 20-3 below.

Listing 20-3. Code snippet showing usage of synchronized keyword in method declaration

Another way to create synchronized code is with synchronized statements. Unlike synchronized methods, synchronized statements must specify the object that provides the intrinsic lock. In Listing 20-4 the statements within the synchronized block will be executed if the block has lock to the executing customer object.

Listing 20-4. Usage of synchronized statements

Apart from the low level concurrency suport that we have seen all this while which Java offers, with Java 5 it also offers high level concurrency support. As part of Java 5 the following concurrent data structures have been introduced:

  • Executors
  • Concurrent Collections
  • Lock Objects
  • Atomic Variables

Since these are the building blocks of concurrency in Java, we will cover these sections in detail in next sections.


Java provides high-level API’s for managing and launching threads. Implementations provided by Java are capable of handling thread pools which can be used in large-scale application having high scalability. In small stand-alone applications it makes sense to mix threading logic with the actual business logic. But for large-scale application it is advisable to separate the threading logic from the core business logic. The objects which encapsulate this threading logic are called executors. To describe executors in detail, I would like to introduce you to some concepts which are very relevant in this context. The following sections describe these concepts in some detail for your understanding.

Executor Interfaces

The three main interfaces in the java.util.concurrent package are as follows:

  • Executor Interface
  • ExecutorService Interface
  • ScheduledExecutorService Interface
Executor Interface

This interface provides a way of decoupling task submission from the mechanics of how each task will be run. An Executor can be used instead of explicitly creating threads and in place of using thread’s start method, executors execute method can be called. Listing 20-5 shows the Executor interface. The Executor interface does not strictly require that execution be asynchronous, instead it can run the submitted task immediately in the caller’s thread.

Listing 20-5. Executor interface source

The command in the execute method will run in a new thread, a pooled thread, or the calling thread, acccording to the Executor implementation.

Page Visitors: 23882

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 *