home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / java / appleevent send and receive / source / shared sources / timer.java < prev    next >
Encoding:
Java Source  |  2000-06-23  |  2.0 KB  |  93 lines

  1. /**
  2.  * Apple Worldwide Developer Technical Support
  3.  *
  4.  * Sample showing how to send and receive AppleEvents using JDirect 2.
  5.  *
  6.  * File: Timer.java
  7.  *
  8.  * A simple timer class.
  9.  * @see TimerCallback
  10.  *
  11.  * @author Levi Brown
  12.  * @author Apple Computer, Inc.
  13.  *
  14.  * Copyright ©1999 Apple Computer, Inc.
  15.  * All rights reserved.
  16.  *
  17.  * @version 1.0
  18.  * 4/15/1999 Shipped as 'AppleEvent Send and Receive' sample.
  19.  *
  20.  * You may incorporate this sample code into your applications without
  21.  * restriction, though the sample code has been provided "AS IS" and the
  22.  * responsibility for its operation is 100% yours.  However, what you are
  23.  * not permitted to do is to redistribute the source as "Apple Sample
  24.  * Code" after having made changes. If you're going to re-distribute the
  25.  * source, we require that you make it clear in the source that the code
  26.  * was descended from Apple Sample Code, but that you've made changes.
  27.  */
  28. public class Timer
  29. {
  30.     /**
  31.      * Instantiates a new Timer.
  32.      * @param the amount of time for the timer to run before calling
  33.      * the callback (time in milliseconds).
  34.      * @param the TimerCallback object to call when the time is up.
  35.      */
  36.     public Timer(int sleepTime, TimerCallback callback)
  37.     {
  38.         this.sleepTime = sleepTime;
  39.         this.callback = callback;
  40.         runObj = new RunObj();
  41.     }
  42.     
  43.     /**
  44.      * Starts the timer running.
  45.      * If the timer is currently running, it will be reset.
  46.      * @see #stop
  47.      */
  48.     public void start()
  49.     {
  50.         if (thread == null || !thread.isAlive())
  51.         {
  52.             thread = new Thread(runObj);
  53.             thread.start();
  54.         }
  55.         else
  56.         {
  57.             thread.stop();
  58.             thread = null;
  59.             start();
  60.         }
  61.     }
  62.     
  63.     /**
  64.      * Stops the timer immediately.
  65.      * @see #start
  66.      */
  67.     public void stop()
  68.     {
  69.         thread.stop();
  70.         thread = null;
  71.     }
  72.     
  73.     /**
  74.      * Simply contains the body of the running thread
  75.      */
  76.     class RunObj implements Runnable
  77.     {
  78.         public void run()
  79.         {
  80.             try
  81.             {
  82.                 Thread.sleep(sleepTime);
  83.                 callback.timeIsUp();
  84.             }
  85.             catch (InterruptedException exc) { }
  86.         }
  87.     }
  88.  
  89.     protected int sleepTime;
  90.     protected TimerCallback callback;
  91.     protected Thread thread;
  92.     protected RunObj runObj;
  93. }