Skip to content
Steve Cote edited this page Sep 18, 2015 · 3 revisions

A Thread Job is unit of work designed to be run in a ThreadPool of worker threads although it can be used in any situation requiring a Runnable class.

This class can be specialized to handle a variety of jobs, but is designed to handle objects that implement the java.lang.Runnable interface. In general, if the (Runnable) class needs to run in a multi-threaded environment, this class defines several utility methods to make life easier.

This class is itself implements Runnable and provides a structured approach to performing work in a multi-threaded environment. It is designed to enable a class to perform work while honoring the concepts of start, stop, suspend and resume and handle many different aspects of running in a multi-threaded environment.

The trade-off is that sub-classes of the ThreadJob must perform all it s work in a re-entrant doWork() method. This method is called repeatedly and several state flags are checked between each call to see if the thread is to suspend or terminate processing.

Sub classes must not spend too long in their doWork() methods and must be able to manage its functions in a re-entrant manner for the ThreadJob to be effective. Blocking in the doWork() method will affect performance and may prevent the entire JRE from terminating normally.

Life Cycle

A ThreadJob has a rather simple life cycle.

Initialize

Immediately upon being run, the initialize method is called. If the sub-class wants to be initialized at the same time this method can be safely over-ridden.

doWork

Once the initialize() method returns, the ThreadJob enters a loop checking the "shutdown" state of the job. While the job is not in a shutdown state, the doWork() method is called and the sub-class can perform its logic.

As mentioned previously, this is a re-entrant method and the sub-class should not block or spend too much time in this method as it will affect performance of the threading ecosystem. The sub-class should perform a unit of work as quickly as possible and return control back to the ThreadJob run loop.

Parking

After the doWork() method returns, the thread yields and then "parks' itself for a short time. This is either a timed wait() call or a more CPU intensive sleep() depending on the setting of certain flags in the ThreadJob. The default is to use the wait() method.

After parking for a short time (controlled by setting the Idle Wait Time attribute), the job returns to the main run loop where the shutdown flag is checked and the doWork() method is called again.

Terminate

When the shutdown loop is terminated (by calling shutdown()), the terminate() method is called. If the sub-class desires to be perform any termination logic, this method should be over-ridden.

Restart

The ThreadJob supports the ability to be restarted. This is possible as the run loop is actually enclosed in a "restart" loop. After the terminate() method is called, the state of the restart flag is checked and if true, the initialize() method is called again and the main run loop is re-entered with the doWork() being called continually (after parking for a short time) until the shutdown() method is called again.

A ThreadJob can be restarted by calling its restart() method. This will set both the restart and shutdown flags to true, interrupts the current thread, and signals all other threads currently blocking to resume so the main run() loop can shutdown, terminate and proceed through the restart loop.

Usage

All that is required to use the ThreadJob is sub-class it, over-ride the doWork() method and have its run() method called. That instance can then be shutdown, restarted, suspended, and resumed.

Clone this wiki locally