Methods Summary |
---|
public static void | addImageSource(DecodingImageSource imgSrc)Adds a new image source to the queue and starts a new loader
thread if required
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 void | beginAnimation()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 void | createLoader()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 void | endAnimation()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 DecodingImageSource | getWaitingImageSource()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.
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 void | run()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());
}
}
|