Concurrent Programming with Java
Lab Manual, Version 1.0, F. Astha Ekadiyanto, 2002.

[Contents] [Next] [Previous]

Lab 4: Introduction to Threads


Schedule the Thread using a Timer Object

About Timer Class

Corresponding to each Timer object is a single background thread that is used to execute all of the timer's tasks, sequentially. Timer tasks should complete quickly. If a timer task takes excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks, which may "bunch up" and execute in rapid succession when (and if) the offending task finally completes.

After the last live reference to a Timer object goes away and all outstanding tasks have completed execution, the timer's task execution thread terminates gracefully (and becomes subject to garbage collection). However, this can take arbitrarily long to occur. By default, the task execution thread does not run as a daemon thread, so it is capable of keeping an application from terminating. If a caller wants to terminate a timer's task execution thread rapidly, the caller should invoke the the timer's cancel method.

If the timer's task execution thread terminates unexpectedly, for example, because its stop method is invoked, any further attempt to schedule a task on the timer will result in an IllegalStateException, as if the timer's cancel method had been invoked.

This class is thread-safe: multiple threads can share a single Timer object without the need for external synchronization. This class does not offer real-time guarantees: it schedules tasks using the Object.wait(long) method.

Implementation note: This class scales to large numbers of concurrently scheduled tasks (thousands should present no problem). Internally, it uses a binary heap to represent its task queue, so the cost to schedule a task is O(log n), where n is the number of concurrently scheduled tasks.

Scheduling Tasks using Timer Objects

A Timer Class has 7 (seven) methods consists of 6 scheduling methods and 1 cancelling method.

Timer Clas Method Summary
 void cancel()
          Terminates this timer, discarding any currently scheduled tasks.
 void schedule(TimerTask task, Date time)
          Schedules the specified task for execution at the specified time.
 void schedule(TimerTask task, Date firstTime, long period)
          Schedules the specified task for repeated fixed-delay execution, beginning at the specified time.
 void schedule(TimerTask task, long delay)
          Schedules the specified task for execution after the specified delay.
 void schedule(TimerTask task, long delay, long period)
          Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.
 void scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
          Schedules the specified task for repeated fixed-rate execution, beginning at the specified time.
 void scheduleAtFixedRate(TimerTask task, long delay, long period)
          Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay.
  

Among these bunch of scheduling methods, we will use the schedule(TimerTask task, long delay, long period) to schedule our MobileTask. The statement for scheduling the task is just as shown below (don't type the code yet since we have still much to do).

 timer.schedule(new MobileTask(ms,x,y),100,100);

What the statement does is:

If you are ready to do some coding again, now let us make the codes.


[Contents] [Next] [Previous]