Beginning Java 2- JDK 1.3 Version Images and Animation
Using Timers
We have adopted a do-it-yourself approach to timing when we need to redraw
in animation. This provides a good insight into how animations operate but
we can accomplish the same result rather more simply by making use of an
object of the Timer class and objects of its associated
TimerTask class. Both classes are defined in the
java.util package. The Timer class we are
discussing here schedules an operation that you define with a
TimerTask object, either once after a given delay, or
repeatedly with a given time interval between successive executions of the
task. The task that is executed by the TimerTask object runs
in a separate thread.
Be aware that there is another Timer class defined in
javax.swing that provides a capability that appears somewhat
similar at first sight but that has significant differences in the way that
it works. The Timer class defined in the
javax.swing package notifies its listeners (of type
ActionListener) when a given time interval has passed, and it
is up to the listener objects to carry out or initiate the task to be
executed. As with the other Timer class, you can use a
Timer object do something just once after a given interval, or
repeatedly after successive intervals of time. A major difference is that
the listener object methods will execute on the same thread unless you
provide code to ensure that is not the case. The Timer and
TimerTask combination of classes provide a way of executing
repeated tasks that is easy to apply to animations so we will concentrate
on those. While we will be applying them to animations here, keep in mind
that you can use these methods for executing any kind of task repeatedly or
after a fixed delay. We will start by looking at how you use a
Timer object to schedule a task.
Timer Objects
You can use a single Timer object to schedule several
different tasks, where each task will be defined by its own
TimerTask object. Each TimerTask object defines a
separate thread, so when you schedule multiple tasks they will each be
executing in a separate thread. The Timer class has been
designed to allow large numbers of tasks to be executed concurrently –
thousands, according to the documentation – without creating undue task
scheduling overhead.
There are two constructors for Timerobjects. The default
constructor simply defines a Timer object that has an
associated thread that is not run as a daemon thread. You will recall that
a daemon thread is subordinate to the thread that created it and dies when
its creator dies, whereas a non-daemon thread runs completely
independently. You can make the Timer thread daemon by
creating the Timer object using the constructor that accepts
an argument of type boolean, and specifying the argument as
true. For instance, the following statement creates a
Timer object with a daemon thread:
Timer clock = new Timer(true); // Create a daemon Timer
When you have no further need of a Timer object, you can
terminate it by calling its cancel() method. Calling the
cancel() method for a Timer object terminates all
tasks scheduled by the timer, and terminates the timer's thread, so you
cannot use the object again for scheduling tasks.
A Timer object provides you with two methods for scheduling
tasks, the schedule() method and the
scheduleAtFixedRate() method. Both of these methods come in
overloaded flavors, so let's look at the schedule() method
first.
The schedule() method is for executing a task either once at a
given instant in time, or repeatedly with each subsequent execution
starting after a fixed delay relative to the previous task. If any
particular execution is delayed, subsequent executions will be delayed.
This mode of repeated task execution is referred to as fixed-delay
execution because priority is given to maintaining the time
interval between task executions, rather than scheduling each execution at
a precise time. This is suitable for applications where the priority is for
repeated executions of a task to be evenly distributed rather than being at
fixed points in time. Animations fall into this category since you will
generally want to have the animation as smooth as possible. You have four
version of the schedule() method available:
schedule(TimerTask task,
Date time)
|
This schedules the task determined by the first argument,
task, to be executed once at the time instant specified by the
second argument, time. If the current time is later than the
time specified by time, then the task executes immediately.
|
schedule(TimerTask task,
Date firstTime,
long period)
|
This schedules the task determined by the first argument,
task, to be executed repeatedly starting at the time specified
by the second argument, firstTime. The third argument,
period, specifies the period in milliseconds between the start
time of one execution of the task and the start time of the next. If the
current time is later than the time specified for the first execution of
the task, then the task executes immediately.
|
schedule(TimerTask task,
long delay)
|
This schedules the task determined by the first argument,
task, to be executed once after a delay relative to the
current time of delay milliseconds.
|
schedule(TimerTask task,
long delay,
long period)
|
This schedules the task determined by the first argument,
task, to be executed repeatedly with the first execution
starting after a delay relative to the current time of delay
milliseconds. The third argument, period, specified the period
in milliseconds between the start time of one execution of the task and the
start time of the next.
|
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|