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

  1. /*
  2.  * @(#)ImageFetcher.java    1.10 95/09/27 Jim Graham
  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.image;
  21.  
  22. import java.util.Vector;
  23.  
  24. class ImageFetcher extends Thread {
  25.     static Thread fetchers[] = new Thread[4];
  26.  
  27.     static final int HIGH_PRIORITY = 8;
  28.     static final int LOW_PRIORITY = 3;
  29.  
  30.     static {
  31.     for (int i = 0; i < fetchers.length; i++) {
  32.         Thread t = fetchers[i] = new ImageFetcher();
  33.         t.setName(t.getName() + " " +i);
  34.         t.setDaemon(true);
  35.         t.start();
  36.     }
  37.     }
  38.  
  39.     private static Vector waitList = new Vector();
  40.  
  41.     private static ThreadGroup getImageFetcherThreadGroup() {
  42.     ThreadGroup g = currentThread().getThreadGroup();
  43.     while (g.getParent() != null) {
  44.         g = g.getParent();
  45.     }
  46.     return g;
  47.     }
  48.  
  49.     private ImageFetcher() {
  50.     super(getImageFetcherThreadGroup(), "Image Fetcher");
  51.     }
  52.  
  53.     public static void add(ImageFetchable src) {
  54.     synchronized(waitList) {
  55.         if (!waitList.contains(src)) {
  56.         waitList.addElement(src);
  57.         waitList.notify();
  58.         }
  59.     }
  60.     }
  61.  
  62.     public static boolean isFetcher(Thread t) {
  63.     for (int i = 0; i < fetchers.length; i++) {
  64.         if (fetchers[i] == t) {
  65.         return true;
  66.         }
  67.     }
  68.     return false;
  69.     }
  70.  
  71.     public static boolean amFetcher() {
  72.     return isFetcher(Thread.currentThread());
  73.     }
  74.  
  75.     private static ImageFetchable nextImage() {
  76.     synchronized(waitList) {
  77.         ImageFetchable src = null;
  78.         while (src == null) {
  79.         while (waitList.size() == 0) {
  80.             try {
  81.             waitList.wait();
  82.             } catch (InterruptedException e) {
  83.             System.err.println("Image Fetcher interrupted!");
  84.             }
  85.         }
  86.         src = (ImageFetchable) waitList.elementAt(0);
  87.         waitList.removeElement(src);
  88.         }
  89.         return src;
  90.     }
  91.     }
  92.  
  93.     public void run() {
  94.     while (true) {
  95.         Thread.currentThread().setPriority(HIGH_PRIORITY);
  96.         ImageFetchable src = nextImage();
  97.         try {
  98.         src.doFetch();
  99.         } catch (Exception e) {
  100.         System.err.println("Uncaught error fetching image:");
  101.         e.printStackTrace();
  102.         }
  103.     }
  104.     }
  105. }
  106.