X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=altoslib%2FAltosMapStore.java;h=eebef310688de8908476aeb020ba715b521141c9;hb=ec20e2f72460010c4f59e6d59775671260e01026;hp=1107bec283333f5812c52ef4692036fa437ff178;hpb=cb23b992be8ba40c97d8988c134a814a13ccd58c;p=fw%2Faltos diff --git a/altoslib/AltosMapStore.java b/altoslib/AltosMapStore.java index 1107bec2..eebef310 100644 --- a/altoslib/AltosMapStore.java +++ b/altoslib/AltosMapStore.java @@ -15,7 +15,7 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -package org.altusmetrum.altoslib_7; +package org.altusmetrum.altoslib_10; import java.io.*; import java.net.*; @@ -118,49 +118,85 @@ public class AltosMapStore { static final long forbidden_interval = 60l * 1000l * 1000l * 1000l; static final long google_maps_ratelimit_ms = 1200; + static Object loader_lock = new Object(); + + static LinkedList waiting = new LinkedList(); + static LinkedList running = new LinkedList(); + + static final int concurrent_loaders = 128; + + static void start_loaders() { + while (!waiting.isEmpty() && running.size() < concurrent_loaders) { + AltosMapStore s = waiting.remove(); + running.add(s); + Thread lt = s.make_loader_thread(); + lt.start(); + } + } + + void finish_loader() { + synchronized(loader_lock) { + running.remove(this); + start_loaders(); + } + } + + void add_loader() { + synchronized(loader_lock) { + waiting.add(this); + start_loaders(); + } + } + class loader implements Runnable { public void run() { - if (file.exists()) { - notify_listeners(AltosMapTile.success); - return; - } - - synchronized(forbidden_lock) { - if (forbidden_set && (System.nanoTime() - forbidden_time) < forbidden_interval) { - notify_listeners(AltosMapTile.forbidden); + try { + if (file.exists()) { + notify_listeners(AltosMapTile.success); return; } - } - int new_status; + synchronized(forbidden_lock) { + if (forbidden_set && (System.nanoTime() - forbidden_time) < forbidden_interval) { + notify_listeners(AltosMapTile.forbidden); + return; + } + } - if (!AltosVersion.has_google_maps_api_key()) { - synchronized (fetch_lock) { - long startTime = System.nanoTime(); - new_status = fetch_url(); - if (new_status == AltosMapTile.success) { - long duration_ms = (System.nanoTime() - startTime) / 1000000; - if (duration_ms < google_maps_ratelimit_ms) { - try { - Thread.sleep(google_maps_ratelimit_ms - duration_ms); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); + int new_status; + + if (!AltosVersion.has_google_maps_api_key()) { + synchronized (fetch_lock) { + long startTime = System.nanoTime(); + new_status = fetch_url(); + if (new_status == AltosMapTile.success) { + long duration_ms = (System.nanoTime() - startTime) / 1000000; + if (duration_ms < google_maps_ratelimit_ms) { + try { + Thread.sleep(google_maps_ratelimit_ms - duration_ms); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } } } } + } else { + new_status = fetch_url(); } - } else { - new_status = fetch_url(); + notify_listeners(new_status); + } finally { + finish_loader(); } - notify_listeners(new_status); } } + private Thread make_loader_thread() { + return new Thread(new loader()); + } + private void load() { - loader l = new loader(); - Thread lt = new Thread(l); - lt.start(); + add_loader(); } private AltosMapStore (String url, File file) { @@ -175,7 +211,18 @@ public class AltosMapStore { } } - public boolean equals(AltosMapStore other) { + public int hashCode() { + return url.hashCode(); + } + + public boolean equals(Object o) { + if (o == null) + return false; + + if (!(o instanceof AltosMapStore)) + return false; + + AltosMapStore other = (AltosMapStore) o; return url.equals(other.url); }