org.norther.tammi.acorn.util.concurrent
Interface ScheduledTimer

All Known Subinterfaces:
Scheduler
All Known Implementing Classes:
DefaultScheduler, DefaultTimeout, SimpleTimer, TimerDaemon

public interface ScheduledTimer

An interface to scheduled timers running scheduled tasks in background.

Author:
Ilkka Priha

Method Summary
 Future executeAfterDelay(Callable command, long delay)
          Excecutes the given callable command after waiting for the given delay.
 Future executeAfterDelay(Runnable command, long delay)
          Excecutes the given command after waiting for the given delay.
 Future executeAt(Callable command, Date date)
          Executes the given callable command at the given time.
 Future executeAt(Runnable command, Date date)
          Executes the given command at the given time.
 Future executePeriodically(Runnable command, Date date, long period)
          Executes the given command every period milliseconds starting at the given time.
 Future executePeriodically(Runnable command, long period, boolean startNow)
          Executes the given command every period milliseconds.
 Future executePeriodically(Runnable command, long delay, long period)
          Executes the given command every period milliseconds after waiting for the given delay.
 Runnable getIdleCommand()
          Gets the idle command.
 long getIdleDelay()
          Gets the idle delay.
 boolean isIdleSupported()
          Checks whether idle commands are supported by this timer.
 void setIdleCommand(Runnable command)
          Sets the idle command.
 void setIdleDelay(long delay)
          Sets the idle delay.
 void start()
          Starts (or restarts) a thread to process commands, or wake up an existing thread if one is already running.
 void stop()
          Cancels all tasks and interrupts the background thread executing the current task, if any.
 

Method Detail

isIdleSupported

boolean isIdleSupported()
Checks whether idle commands are supported by this timer.

Returns:
true if idle is supported, false otherwise.

getIdleCommand

Runnable getIdleCommand()
Gets the idle command.

Returns:
the idle command or null.

setIdleCommand

void setIdleCommand(Runnable command)
Sets the idle command.

Parameters:
command - the idle command.

getIdleDelay

long getIdleDelay()
Gets the idle delay.

Returns:
the idle delay in msecs.

setIdleDelay

void setIdleDelay(long delay)
Sets the idle delay.

Parameters:
delay - the idle delay in msecs.

executeAt

Future executeAt(Runnable command,
                 Date date)
Executes the given command at the given time.

Parameters:
command - the command to run at the given time.
date - the absolute time to run the command.
Returns:
a cancellable task.

executeAfterDelay

Future executeAfterDelay(Runnable command,
                         long delay)
Excecutes the given command after waiting for the given delay.

Sample Usage. You can use a ClockDaemon to arrange timeout callbacks to break out of stuck IO. For example (code sketch):

 class X
 {
     ...
     ScheduledTimer timer = ...
     Thread readerThread;
     FileInputStream datafile;
  
     void startReadThread()
     {
         datafile = new FileInputStream("data", ...);
         readerThread = new Thread(new Runnable()
         {
             public void run()
             {
                 for(;;)
                 {
                     // Try to gracefully exit before blocking.
                     if (Thread.currentThread().isInterrupted())
                     {
                         quietlyWrapUpAndReturn();
                     }
                     else
                     {
                         try
                         {
                             int c = datafile.read();
                             if (c == -1)
                             {
                                 break;
                             }
                             else
                             {
                                 process(c);
                             }
                         }
                         catch (IOException x)
                         {
                             cleanup();
                             return;
                         }
                     }
                 }
             }
         };
         readerThread.start();
         // Establish callback to cancel after 60 seconds.
         timer.executeAfterDelay(new Runnable()
         {
             public void run()
             {
                 readerThread.interrupt();  // try to interrupt thread
                 datafile.close();  // force thread to lose its input file
             }
         }, 60000);
     }
 }
 

Parameters:
command - the command to run after the delay.
delay - the delay from now to run the command in msecs.
Returns:
a cancellable task.

executePeriodically

Future executePeriodically(Runnable command,
                           long period,
                           boolean startNow)
Executes the given command every period milliseconds. If startNow is true, execution begins immediately, otherwise, it begins after the first period delay.

Sample Usage . Here is one way to update Swing components acting as progress indicators for long-running actions.

 class X
 {
     JLabel statusLabel = ...;
     int percentComplete = 0;
     synchronized int  getPercentComplete() { return percentComplete; }
     synchronized void setPercentComplete(int p) { percentComplete = p; }
  
     ScheduledTimer timer = ...;
     void startWorking()
     {
         Runnable showPct = new Runnable()
         {
             public void run()
             {
                 SwingUtilities.invokeLater(new Runnable()
                 {
                     public void run()
                     {
                         statusLabel.
                             setText(getPercentComplete() + "%");
                     }
                 }
             }
         };
  
         final Future updater = timer.executePeriodically(showPct, 500, true);
         Runnable action = new Runnable()
         {
             public void run()
             {
                 for (int i = 0; i < 100; ++i)
                 {
                     work();
                     setPercentComplete(i);
                 }
                 updater.cancel();
             }
         };
  
         new Thread(action).start();
     }
 }
 

Parameters:
command - the command to run at each cycle.
period - the period, in msecs. Periods are measured from start-of-task to the next start-of-task. It is generally a bad idea to use a period that is shorter than the expected task duration.
startNow - true if the cycle should start with execution of the task now. Otherwise, the cycle starts with a delay of period milliseconds.
Returns:
a cancellable task.
Throws:
IllegalArgumentException - if period less than or equal to zero.

executePeriodically

Future executePeriodically(Runnable command,
                           Date date,
                           long period)
Executes the given command every period milliseconds starting at the given time.

Parameters:
command - the runnable to execute.
date - the startnig date.
period - the period.
Returns:
a cancellable task.
Throws:
IllegalArgumentException - if period less than or equal to zero.

executePeriodically

Future executePeriodically(Runnable command,
                           long delay,
                           long period)
Executes the given command every period milliseconds after waiting for the given delay.

Parameters:
command - the runnable to execute.
delay - the delay in msecs.
period - the period.
Returns:
a cancellable task.
Throws:
IllegalArgumentException - if period less than or equal to zero.

executeAt

Future executeAt(Callable command,
                 Date date)
Executes the given callable command at the given time.

Parameters:
command - the command to run at the given time.
date - the absolute time to run the command.
Returns:
a cancellable task.

executeAfterDelay

Future executeAfterDelay(Callable command,
                         long delay)
Excecutes the given callable command after waiting for the given delay.

Parameters:
command - the command to run after the delay.
delay - the delay from now to run the command in msecs.
Returns:
a cancellable task.

start

void start()
           throws Exception
Starts (or restarts) a thread to process commands, or wake up an existing thread if one is already running. This method can be invoked if the background thread crashed due to an unrecoverable exception in an executed command.

Throws:
Exception - if starts fails.

stop

void stop()
Cancels all tasks and interrupts the background thread executing the current task, if any. A new background thread will be started if new execution requests are encountered. If the currently executing task does not repsond to interrupts, the current thread may persist, even if a new thread is started via restart().



Copyright © 2004 The Norther Organization. All rights reserved.