FileDocCategorySizeDatePackage
ImageLoader.javaAPI DocAndroid 1.5 API6846Wed May 06 22:41:54 BST 2009org.apache.harmony.awt.gl.image

ImageLoader

public class ImageLoader extends Thread
This class provides functionality for simultaneous loading of several images and running animation.

Fields Summary
Constructors Summary
ImageLoader()

        super();
        setDaemon(true);
    
Methods Summary
public static voidaddImageSource(DecodingImageSource imgSrc)
Adds a new image source to the queue and starts a new loader thread if required

param
imgSrc - image source

        ImageLoadersStorage storage = ImageLoadersStorage.getStorage();
        synchronized(storage.queue) {
            if (!storage.queue.contains(imgSrc)) {
                storage.queue.add(imgSrc);
            }
            if (storage.freeLoaders == 0) {
                createLoader();
            }

            storage.queue.notify();
        }
    
static voidbeginAnimation()
Removes current thread from loaders (so we are able to create more loaders) and decreases its priority.

        ImageLoadersStorage storage = ImageLoadersStorage.getStorage();
        Thread currThread = Thread.currentThread();

        synchronized(storage) {
            storage.loaders.remove(currThread);

            if (storage.freeLoaders < storage.queue.size()) {
                createLoader();
            }
        }

        currThread.setPriority(Thread.MIN_PRIORITY);
    
private static voidcreateLoader()
This method creates a new thread which is able to load an image or run animation (if the number of existing loader threads does not exceed the limit).

        final ImageLoadersStorage storage = ImageLoadersStorage.getStorage();

        synchronized(storage.loaders) {
            if (storage.loaders.size() < ImageLoadersStorage.MAX_THREADS) {
                AccessController.doPrivileged(
                        new PrivilegedAction<Void>() {
                            public Void run() {
                                ImageLoader loader = new ImageLoader();
                                storage.loaders.add(loader);
                                loader.start();
                                return null;
                            }
                        });
            }
        }
    
static voidendAnimation()
Sends the current thread to wait for the new images to load if there are free placeholders for loaders

        ImageLoadersStorage storage = ImageLoadersStorage.getStorage();
        Thread currThread = Thread.currentThread();

        synchronized(storage) {
            if (storage.loaders.size() < ImageLoadersStorage.MAX_THREADS &&
                    !storage.loaders.contains(currThread)
            ) {
                storage.loaders.add(currThread);
            }
        }

        currThread.setPriority(Thread.NORM_PRIORITY);
    
private static DecodingImageSourcegetWaitingImageSource()
Waits for a new ImageSource until timout expires. Loader thread will terminate after returning from this method if timeout expired and image source was not picked up from the queue.

return
image source picked up from the queue or null if timeout expired

        ImageLoadersStorage storage = ImageLoadersStorage.getStorage();

        synchronized(storage.queue) {
            DecodingImageSource isrc = null;

            if (storage.queue.size() == 0) {
                try {
                    storage.freeLoaders++;
                    storage.queue.wait(ImageLoadersStorage.TIMEOUT);
                } catch (InterruptedException e) {
                    return null;
                } finally {
                    storage.freeLoaders--;
                }
            }

            if (storage.queue.size() > 0) {
                isrc = storage.queue.get(0);
                storage.queue.remove(0);
            }

            return isrc;
        }
    
public voidrun()
Entry point of the loader thread. Picks up image sources and runs decoders for them while there are available image sources in the queue. If there are no and timeout expires it terminates.

        ImageLoadersStorage storage = ImageLoadersStorage.getStorage();

        try {
            while (storage.loaders.contains(this)) {
                Thread.interrupted(); // Reset the interrupted flag
                DecodingImageSource isrc = getWaitingImageSource();
                if (isrc != null) {
                    try {
                        isrc.load();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    break; // Don't wait if timeout expired - terminate loader
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            synchronized(storage.loaders) {
                storage.loaders.remove(Thread.currentThread());
            }
        }