11.1 Overview of Task Scheduling
Interestingly, this is not the first
time
that we have been concerned with when a task is to be executed.
Previously, we've just considered the timing as part
of the task. We've seen tools that allow threads to
wait for specific periods of time. Here is a quick review:
- The sleep()
method
-
In our discussion of the Thread class, we examined
the concept of a thread waiting for a specific period of time. The
purpose was either to allow other threads to accomplish related
tasks, to allow external events to happen during the sleeping period,
or to repeat a task periodically. The tasks that are listed after the
sleep() method are executed at a later time
period. In effect, the sleep() method controls
when those tasks are executed.
- The join()
method
-
Our discussion of this method of the Thread class
represents the first time that we examined alternate tasks to be
executed at a later time. The goal of this method is to wait for a
specific event梐 thread termination. However, the expected
thread termination event may not arrive, at least not within the
desired time period, so the join() method
provides a timeout. This allows the method to return梕ither by
the termination of the thread or by the expiration of the
timeout梩hus allowing the program to execute an alternate task
at a specific time and in a particular situation.
- The wait()
method
-
The wait() method of the
Object class allows a thread to wait for any
event. This method also provides the option to return if a specific
time period passes. This allows the program to execute a task at a
later time if the event occurs or to specify the exact time to
execute an alternate task if the event does not occur. This
functionality is also emulated with condition variables using the
await() method.
- The TimeUnit class
-
This class is used to define a time period, allowing methods to
specify a time period in units other than milliseconds or
nanoseconds. This class is used by many of the classes added in J2SE
5.0 to specify a time period for a timeout. This class also provides
convenience methods to support certain periodic
requests梥pecifically, it provides alternate implementations of
the sleep(), join(), and
wait() methods that use a
TimeUnit object as their timeout argument.
- The DelayQueue class
-
Our discussion of the DelayQueue class in Chapter 8 is the first time we encounter a class that
allows data to be processed at a specific time. When a producer
places data in a delay queue, it is not readable by consumers until
after a specific period passes. In effect, the task to process the
data is to be executed at a later time梐 time period that is
specified by the data itself.
As these examples show, in some cases, a program needs to execute
code only after a specific event or after a period of time. Much of
the time, the functionality is indirect in that the timeout is not
expected to occur. Java also supports timeout functions directly by
providing tools that allow the program to execute specific tasks at a
specific time.
We've used these methods in our examples when a
program needs to execute code only after a specific event or after a
period of time. The timing in these cases has always been provided as
a timeout value: after a certain period of time, the thread would
regain control and be able to execute the appropriate task. However,
in this case control always resides with the thread: execution of the
appropriate task is synchronous with respect to the code being
executed. Java also supports asynchronous task execution in alternate
threads; it's that type of execution that
we'll examine in the remainder of this chapter.
|