home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / sun / awt / image / ImageFetcher.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  3.4 KB  |  119 lines

  1. package sun.awt.image;
  2.  
  3. import java.util.Vector;
  4.  
  5. class ImageFetcher extends Thread {
  6.    static Thread[] fetchers = null;
  7.    static final int HIGH_PRIORITY = 8;
  8.    static final int LOW_PRIORITY = 3;
  9.    private static Vector waitList;
  10.  
  11.    private static ThreadGroup getImageFetcherThreadGroup() {
  12.       ThreadGroup g;
  13.       for(g = Thread.currentThread().getThreadGroup(); g.getParent() != null && g.getParent().getParent() != null; g = g.getParent()) {
  14.       }
  15.  
  16.       return g;
  17.    }
  18.  
  19.    private ImageFetcher() {
  20.       super(getImageFetcherThreadGroup(), "Image Fetcher");
  21.    }
  22.  
  23.    public static void add(ImageFetchable src) {
  24.       Vector var1 = waitList;
  25.       synchronized(var1){}
  26.  
  27.       try {
  28.          if (!waitList.contains(src)) {
  29.             waitList.addElement(src);
  30.             waitList.notify();
  31.          }
  32.       } catch (Throwable var3) {
  33.          throw var3;
  34.       }
  35.  
  36.    }
  37.  
  38.    public static boolean isFetcher(Thread t) {
  39.       for(int i = 0; i < fetchers.length; ++i) {
  40.          if (fetchers[i] == t) {
  41.             return true;
  42.          }
  43.       }
  44.  
  45.       return false;
  46.    }
  47.  
  48.    public static boolean amFetcher() {
  49.       return isFetcher(Thread.currentThread());
  50.    }
  51.  
  52.    private static ImageFetchable nextImage() {
  53.       Vector var0 = waitList;
  54.       synchronized(var0){}
  55.  
  56.       ImageFetchable var3;
  57.       try {
  58.          ImageFetchable src = null;
  59.  
  60.          while(src == null) {
  61.             while(waitList.size() == 0) {
  62.                try {
  63.                   waitList.wait();
  64.                } catch (InterruptedException var6) {
  65.                   System.err.println("Image Fetcher interrupted!");
  66.                }
  67.             }
  68.  
  69.             src = (ImageFetchable)waitList.elementAt(0);
  70.             waitList.removeElement(src);
  71.          }
  72.  
  73.          var3 = src;
  74.       } catch (Throwable var7) {
  75.          throw var7;
  76.       }
  77.  
  78.       return var3;
  79.    }
  80.  
  81.    public void run() {
  82.       while(true) {
  83.          SecurityManager.setScopePermission();
  84.          Thread.currentThread().setPriority(8);
  85.          SecurityManager.resetScopePermission();
  86.          ImageFetchable src = nextImage();
  87.  
  88.          try {
  89.             src.doFetch();
  90.             ImageFetchable var4 = null;
  91.          } catch (Exception e) {
  92.             System.err.println("Uncaught error fetching image:");
  93.             ((Throwable)e).printStackTrace();
  94.          }
  95.       }
  96.    }
  97.  
  98.    static {
  99.       SecurityManager.setScopePermission();
  100.       String imageFetcherCountString = System.getProperty("awt.imagefetchers", "4");
  101.       SecurityManager.resetScopePermission();
  102.       int imageFetcherCount = Integer.parseInt(imageFetcherCountString);
  103.       SecurityManager.setScopePermission();
  104.       fetchers = new Thread[imageFetcherCount];
  105.       SecurityManager.resetScopePermission();
  106.  
  107.       for(int i = 0; i < fetchers.length; ++i) {
  108.          SecurityManager.setScopePermission();
  109.          Thread t = fetchers[i] = new ImageFetcher();
  110.          t.setName(t.getName() + " " + i);
  111.          t.setDaemon(true);
  112.          SecurityManager.resetScopePermission();
  113.          t.start();
  114.       }
  115.  
  116.       waitList = new Vector();
  117.    }
  118. }
  119.