jdk/src/share/classes/sun/awt/image/ImageFetcher.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8353 2834083cdb2c
child 22567 5816a47fa4dd
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8353
diff changeset
     2
 * Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt.image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
  * An ImageFetcher is a thread used to fetch ImageFetchable objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
  * Once an ImageFetchable object has been fetched, the ImageFetcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
  * thread may also be used to animate it if necessary, via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
  * startingAnimation() / stoppingAnimation() methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
  * There can be up to FetcherInfo.MAX_NUM_FETCHERS_PER_APPCONTEXT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
  * ImageFetcher threads for each AppContext.  A per-AppContext queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
  * of ImageFetchables is used to track objects to fetch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
  * @author Jim Graham
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
  * @author Fred Ecks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
class ImageFetcher extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static final int HIGH_PRIORITY = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static final int LOW_PRIORITY = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static final int ANIM_PRIORITY = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static final int TIMEOUT = 5000; // Time in milliseconds to wait for an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                                     // ImageFetchable to be added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                                     // queue before an ImageFetcher dies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
      * Constructor for ImageFetcher -- only called by add() below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private ImageFetcher(ThreadGroup threadGroup, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        super(threadGroup, "Image Fetcher " + index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
      * Adds an ImageFetchable to the queue of items to fetch.  Instantiates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
      * a new ImageFetcher if it's reasonable to do so.
8353
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
    64
      * If there is no available fetcher to process an ImageFetchable, then
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
    65
      * reports failure to caller.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
      */
8353
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
    67
    public static boolean add(ImageFetchable src) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        final FetcherInfo info = FetcherInfo.getFetcherInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        synchronized(info.waitList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            if (!info.waitList.contains(src)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                info.waitList.addElement(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                if (info.numWaiting == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                            info.numFetchers < info.fetchers.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    createFetchers(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                }
8353
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
    76
                /* Creation of new fetcher may fail due to high vm load
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
    77
                 * or some other reason.
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
    78
                 * If there is already exist, but busy, fetcher, we leave
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
    79
                 * the src in queue (it will be handled by existing
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
    80
                 * fetcher later).
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
    81
                 * Otherwise, we report failure: there is no fetcher
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
    82
                 * to handle the src.
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
    83
                 */
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
    84
                if (info.numFetchers > 0) {
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
    85
                    info.waitList.notify();
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
    86
                } else {
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
    87
                    info.waitList.removeElement(src);
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
    88
                    return false;
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
    89
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
8353
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
    92
        return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
      * Removes an ImageFetchable from the queue of items to fetch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public static void remove(ImageFetchable src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        final FetcherInfo info = FetcherInfo.getFetcherInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        synchronized(info.waitList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (info.waitList.contains(src)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                info.waitList.removeElement(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
      * Checks to see if the given thread is one of the ImageFetchers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public static boolean isFetcher(Thread t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        final FetcherInfo info = FetcherInfo.getFetcherInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        synchronized(info.waitList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            for (int i = 0; i < info.fetchers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                if (info.fetchers[i] == t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
      * Checks to see if the current thread is one of the ImageFetchers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public static boolean amFetcher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return isFetcher(Thread.currentThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
      * Returns the next ImageFetchable to be processed.  If TIMEOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
      * elapses in the mean time, or if the ImageFetcher is interrupted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
      * null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private static ImageFetchable nextImage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        final FetcherInfo info = FetcherInfo.getFetcherInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        synchronized(info.waitList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            ImageFetchable src = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            long end = System.currentTimeMillis() + TIMEOUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            while (src == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                while (info.waitList.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    long now = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    if (now >= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        info.numWaiting++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        info.waitList.wait(end - now);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        // A normal occurrence as an AppContext is disposed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        info.numWaiting--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                src = (ImageFetchable) info.waitList.elementAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                info.waitList.removeElement(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            return src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
      * The main run() method of an ImageFetcher Thread.  Calls fetchloop()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
      * to do the work, then removes itself from the array of ImageFetchers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        final FetcherInfo info = FetcherInfo.getFetcherInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            fetchloop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            synchronized(info.waitList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                Thread me = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                for (int i = 0; i < info.fetchers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    if (info.fetchers[i] == me) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        info.fetchers[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        info.numFetchers--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
      * The main ImageFetcher loop.  Repeatedly calls nextImage(), and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
      * fetches the returned ImageFetchable objects until nextImage()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
      * returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    private void fetchloop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        Thread me = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        while (isFetcher(me)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            // we're ignoring the return value and just clearing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            // the interrupted flag, instead of bailing out if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            // the fetcher was interrupted, as we used to,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            // because there may be other images waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            // to be fetched (see 4789067)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            me.interrupted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            me.setPriority(HIGH_PRIORITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            ImageFetchable src = nextImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (src == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                src.doFetch();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                System.err.println("Uncaught error fetching image:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            stoppingAnimation(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
      * Recycles this ImageFetcher thread as an image animator thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
      * Removes this ImageFetcher from the array of ImageFetchers, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
      * resets the thread name to "ImageAnimator".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    static void startingAnimation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        final FetcherInfo info = FetcherInfo.getFetcherInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        Thread me = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        synchronized(info.waitList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            for (int i = 0; i < info.fetchers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                if (info.fetchers[i] == me) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    info.fetchers[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    info.numFetchers--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    me.setName("Image Animator " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    if(info.waitList.size() > info.numWaiting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                       createFetchers(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        me.setPriority(ANIM_PRIORITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        me.setName("Image Animator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
      * Returns this image animator thread back to service as an ImageFetcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
      * if possible.  Puts it back into the array of ImageFetchers and sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
      * the thread name back to "Image Fetcher".  If there are already the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
      * maximum number of ImageFetchers, this method simply returns, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
      * fetchloop() will drop out when it sees that this thread isn't one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
      * the ImageFetchers, and this thread will die.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    private static void stoppingAnimation(Thread me) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        final FetcherInfo info = FetcherInfo.getFetcherInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        synchronized(info.waitList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            int index = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            for (int i = 0; i < info.fetchers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                if (info.fetchers[i] == me) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                if (info.fetchers[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    index = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            if (index >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                info.fetchers[index] = me;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                info.numFetchers++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                me.setName("Image Fetcher " + index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
      * Create and start ImageFetcher threads in the appropriate ThreadGroup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    private static void createFetchers(final FetcherInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
       // We need to instantiate a new ImageFetcher thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
       // First, figure out which ThreadGroup we'll put the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
       // new ImageFetcher into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
       final AppContext appContext = AppContext.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
       ThreadGroup threadGroup = appContext.getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
       ThreadGroup fetcherThreadGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
       try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
          if (threadGroup.getParent() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
             // threadGroup is not the root, so we proceed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
             fetcherThreadGroup = threadGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
          } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
             // threadGroup is the root ("system") ThreadGroup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
             // We instead want to use its child: the "main"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
             // ThreadGroup.  Thus, we start with the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
             // ThreadGroup, and go up the tree until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
             // threadGroup.getParent().getParent() == null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
             threadGroup = Thread.currentThread().getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
             ThreadGroup parent = threadGroup.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
             while ((parent != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                  && (parent.getParent() != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                  threadGroup = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                  parent = threadGroup.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
             fetcherThreadGroup = threadGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
       } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
         // Not allowed access to parent ThreadGroup -- just use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
         // the AppContext's ThreadGroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
         fetcherThreadGroup = appContext.getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
       final ThreadGroup fetcherGroup = fetcherThreadGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
       java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
         new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
         public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
             for (int i = 0; i < info.fetchers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
               if (info.fetchers[i] == null) {
8353
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
   310
                   ImageFetcher f = new ImageFetcher(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                           fetcherGroup, i);
8353
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
   312
                   try {
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
   313
                       f.start();
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
   314
                       info.fetchers[i] = f;
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
   315
                       info.numFetchers++;
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
   316
                       break;
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
   317
                   } catch (Error e) {
2834083cdb2c 6818960: ImageFetcher ( MediaTracker) Thread leak
bae
parents: 5506
diff changeset
   318
                   }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
          return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
       });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
  * The FetcherInfo class encapsulates the per-AppContext ImageFetcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
  * information.  This includes the array of ImageFetchers, as well as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
  * the queue of ImageFetchable objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
class FetcherInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    static final int MAX_NUM_FETCHERS_PER_APPCONTEXT = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    Thread[] fetchers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    int numFetchers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    int numWaiting;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    Vector waitList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    private FetcherInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        fetchers = new Thread[MAX_NUM_FETCHERS_PER_APPCONTEXT];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        numFetchers = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        numWaiting = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        waitList = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /* The key to put()/get() the FetcherInfo into/from the AppContext. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    private static final Object FETCHER_INFO_KEY =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                        new StringBuffer("FetcherInfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    static FetcherInfo getFetcherInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        AppContext appContext = AppContext.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        synchronized(appContext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            FetcherInfo info = (FetcherInfo)appContext.get(FETCHER_INFO_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            if (info == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                info = new FetcherInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                appContext.put(FETCHER_INFO_KEY, info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            return info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
}