Spring Book – Chapter 19 – Tasks and Scheduling

Concurrent programming and management of threads in general are necessary because the world of web programming is essentially concurrent because multiple clients can access in the time. Asynchronous programming promotes using the same thread to process multiple requests, sequentially, but with no request blocking the thread.

Spring has supported task scheduling and asynchronous method execution from its earliest releases. Spring 3.0 has made this easier by allowing you to declaratively do this by using XML and annotation-based programming.

This chapter initially looks into the various concepts dealing with concurrency and then looks into Java support for doing this. Later on it looks into how Spring Framework can support doing this in your application.

Terms and Concepts

The following concepts and terms that will be used throughout the chapter :

Concurrency Concurrency is a system property in which several computations are executing simultaneously, and potentially interacting with each other. Concurrency is a built in feature of the Java language and platform.

Process A process is a flow sequence that runs independently and isolated from other processes. It cannot have access to data in other processes.

Thread A thread is a single, sequential flow of control within a program. A thread can be called as a lightweight process which has access to shared data. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently. Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread.

Task A task is a basic unit of programming. In our sample application, downloading the cargo utilization report of the first hundred customers represents a simple task.

Multithreading Multithreading is the ability of a program to manage its use by more than one thread at the same time and to even handle multiple requests, without having to have multiple copies of the program running. Each execution of the program is kept track of as a thread with a separate identity.

Multitasking Multitasking is the ability of a program to allow a thread to perform more than one task at a time. The program can keep track of where you are in these tasks and go from one to the other without losing information. Being able to do multitasking doesn’t mean that an unlimited number of tasks can be performed at the same time. Each task consumes system storage and other resources and as more tasks are started, the system may slow down or begin to run out of storage.

Serial Processing Serial processing is strictly sequential, without overlap of the successive processing times on objects or distinct subsystems. In a standard type of serial system, each object takes the same average amount of time to process and the next object begins processing only when the previous one is completed.

Parallel Processing Parallel processing signifies simultaneous processing on several objects or subsystems at the same time, although processing may finish on different objects at different times. Parallelism is a term typically used to describe executions that physically execute simultaneously with the goal of solving a problem in less time or solving a larger problem in the same time. Parallelism differs from concurrency and actually exploits concurrency. The advantages of parallel processing are faster execution time and higher throughput. The disadvantages are that there are additional hardware and power requirements.

Task Scheduling This is another form of concurrency in action, wherein a piece of work can be scheduled to run once or repeatedly at an interval for various application use cases. In our sample application a task schedule can be created for sending periodic mails to the entire customer in the system giving details of their cargo loyalty points.

Note: Scheduling is needed if you want your application to automate the repetition of a task at specific intervals or particular date. In any modern application its common to have scheduling requirements, and the Spring Framework brings a level of abstraction to this as well. Spring has good support for scheduling and, as shown later in this chapter, it also supports using other third party scheduling services, such as Quartz.

Liveness Liveness defines a concurrent application’s ability to execute in a timely manner. There are various kinds of liveness problems that can occur in your application, namely deadlock, starvation and livelock.

Deadlock An application that allows concurrency is prone to a deadlock condition in which all the threads are waiting for an event that other threads in the same set have to cause. For example, if thread A waits for a lock on object C, which thread B holds, and thread B waits for a lock on object D, which is held by thread A, these two threads are locked and cannot continue further, resulting in deadlock.

Starvation Starvation describes a situation where a thread is unable to gain regular access to shared resources and is unable to make progress by itself. This happens when shared resources are made unavailable for long periods by other “greedy” threads.

Livelock A thread often acts in response to the action of another thread. If the other thread’s action is also a response to the action of another thread, then livelock may result. As with deadlock, livelocked threads are unable to make further progress, but unlike deadlock they are not blocked. They are simply too busy responding to each other to resume with their own work.

Currency Support by Java

Concurrency is a built in feature of the Java language and platform. Java has a predefined class java.lang.Thread which provides the mechanism by which threads are created. However to avoid all threads having to be child classes of Thread, it also uses a standard interface java.lang.Runnable. Any class which wishes to express concurrent execution can implement Runnable interface and provide the run method. Threads do not begin their execution until the start method in the Thread class is called. Listing 20-1 shows creating a thread by extending Thread class and Listing 20-2 shows creating of thread by implementing the Runnable interface.

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.