home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / awt / ScreenUpdater.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  4.5 KB  |  166 lines

  1. /*
  2.  * @(#)ScreenUpdater.java    1.14 96/02/29  
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package sun.awt;
  21.  
  22. class ScreenUpdaterEntry {
  23.     UpdateClient client;
  24.     long when;
  25.     ScreenUpdaterEntry next;
  26.     Object arg;
  27.  
  28.     ScreenUpdaterEntry(UpdateClient client, long when, Object arg, ScreenUpdaterEntry next) {
  29.     this.client = client;
  30.     this.when = when;
  31.     this.arg = arg;
  32.     this.next = next;
  33.     }
  34. }
  35.  
  36. /**
  37.  * A seperate low priority thread that warns clients
  38.  * when they need to update the screen. Clients that
  39.  * need a wakeup call need to call Notify().
  40.  *
  41.  * @version     1.14, 02/29/96
  42.  * @author     Arthur van Hoff
  43.  */
  44. public
  45. class ScreenUpdater extends Thread {
  46.     private ScreenUpdaterEntry first;
  47.  
  48.     /**
  49.      * The screen updater. 
  50.      */
  51.     public final static ScreenUpdater updater = new ScreenUpdater();
  52.  
  53.     private static ThreadGroup getScreenUpdaterThreadGroup() {
  54.     ThreadGroup g = currentThread().getThreadGroup();
  55.     while (g.getParent() != null) {
  56.         g = g.getParent();
  57.     }
  58.     return g;
  59.     }
  60.  
  61.     /**
  62.      * Constructor. Starts the thread.
  63.      */
  64.     private ScreenUpdater() {
  65.     super(getScreenUpdaterThreadGroup(), "Screen Updater");
  66.     start();
  67.     }
  68.  
  69.     /**
  70.      * Update the next client
  71.      */
  72.     private synchronized ScreenUpdaterEntry nextEntry() throws InterruptedException {
  73.     while (true) {
  74.         if (first == null) {
  75.         wait();
  76.         continue;
  77.         }
  78.         long delay = first.when - System.currentTimeMillis();
  79.         if (delay <= 0) {
  80.         ScreenUpdaterEntry entry = first;
  81.         first = first.next;
  82.         return entry;
  83.         }
  84.         wait(delay);
  85.     }
  86.     }
  87.  
  88.     /**
  89.      * The main body of the screen updater.
  90.      */
  91.     public void run() {
  92.     try {
  93.         while (true) {
  94.         setPriority(NORM_PRIORITY - 1);
  95.         ScreenUpdaterEntry entry = nextEntry();
  96.         setPriority(NORM_PRIORITY);
  97.  
  98.         try {
  99.             entry.client.updateClient(entry.arg);
  100.         } catch (Throwable e) {
  101.             e.printStackTrace();
  102.         }
  103.  
  104.         // Because this thread is permanent, we don't want to defeat
  105.         // the garbage collector by having dangling references to
  106.         // objects we no longer care about. Clear out entry so that
  107.         // when we go back to sleep in nextEntry we won't hold a
  108.         // dangling reference to the previous entry we processed.
  109.         entry = null;
  110.         }
  111.     } catch (InterruptedException e) {
  112.     }
  113.     }
  114.  
  115.     /**
  116.      * Notify the screen updater that a client needs
  117.      * updating. As soon as the screen updater is
  118.      * scheduled to run it will ask all of clients that
  119.      * need updating to update the screen.
  120.      */
  121.     public void notify(UpdateClient client) {
  122.     notify(client, 100, null);
  123.     }
  124.     public synchronized void notify(UpdateClient client, long delay) {
  125.     notify(client, delay, null);
  126.     }
  127.     public synchronized void notify(UpdateClient client, long delay, Object arg) {
  128.     long when = System.currentTimeMillis() + delay;
  129.     long nextwhen = (first != null) ? first.when : -1L;
  130.  
  131.     if (first != null) {
  132.         if ((first.client == client) && (first.arg == arg)) {
  133.         if (when >= first.when) {
  134.             return;
  135.         }
  136.         first = first.next;
  137.         } else {
  138.         for (ScreenUpdaterEntry e = first ; e.next != null ; e = e.next) {
  139.             if ((e.next.client == client) && (e.next.arg == arg)) {
  140.             if (when >= e.next.when) {
  141.                 return;
  142.             }
  143.             e.next = e.next.next;
  144.             break;
  145.             }
  146.         }
  147.         }
  148.     }
  149.  
  150.     if ((first == null) || (first.when > when)) {
  151.         first = new ScreenUpdaterEntry(client, when, arg, first);
  152.     } else {
  153.         for (ScreenUpdaterEntry e = first ; ; e = e.next) {
  154.         if ((e.next == null) || (e.next.when > when)) {
  155.             e.next = new ScreenUpdaterEntry(client, when, arg, e.next);
  156.             break;
  157.         }
  158.         }
  159.     }
  160.     if (nextwhen != first.when) {
  161.         super.notify();
  162.     }
  163.     }
  164. }
  165.  
  166.