Skip to content

Commit 6619f14

Browse files
jeffdgr8barbeau
authored andcommitted
Use thread pools (#601)
Reuse threads from pool instead of creating new thread for every task
1 parent f6ce317 commit 6619f14

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

library/src/main/java/com/google/maps/android/clustering/algo/PreCachingAlgorithmDecorator.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
import java.util.Collection;
2525
import java.util.Set;
26+
import java.util.concurrent.Executor;
27+
import java.util.concurrent.Executors;
2628
import java.util.concurrent.locks.ReadWriteLock;
2729
import java.util.concurrent.locks.ReentrantReadWriteLock;
2830

@@ -35,6 +37,7 @@ public class PreCachingAlgorithmDecorator<T extends ClusterItem> extends Abstrac
3537
// TODO: evaluate maxSize parameter for LruCache.
3638
private final LruCache<Integer, Set<? extends Cluster<T>>> mCache = new LruCache<Integer, Set<? extends Cluster<T>>>(5);
3739
private final ReadWriteLock mCacheLock = new ReentrantReadWriteLock();
40+
private final Executor mExecutor = Executors.newCachedThreadPool();
3841

3942
public PreCachingAlgorithmDecorator(Algorithm<T> algorithm) {
4043
mAlgorithm = algorithm;
@@ -80,10 +83,10 @@ public Set<? extends Cluster<T>> getClusters(float zoom) {
8083
Set<? extends Cluster<T>> results = getClustersInternal(discreteZoom);
8184
// TODO: Check if requests are already in-flight.
8285
if (mCache.get(discreteZoom + 1) == null) {
83-
new Thread(new PrecacheRunnable(discreteZoom + 1)).start();
86+
mExecutor.execute(new PrecacheRunnable(discreteZoom + 1));
8487
}
8588
if (mCache.get(discreteZoom - 1) == null) {
86-
new Thread(new PrecacheRunnable(discreteZoom - 1)).start();
89+
mExecutor.execute(new PrecacheRunnable(discreteZoom - 1));
8790
}
8891
return results;
8992
}

library/src/main/java/com/google/maps/android/clustering/view/DefaultClusterRenderer.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
import java.util.Queue;
6565
import java.util.Set;
6666
import java.util.concurrent.ConcurrentHashMap;
67+
import java.util.concurrent.Executor;
68+
import java.util.concurrent.Executors;
6769
import java.util.concurrent.locks.Condition;
6870
import java.util.concurrent.locks.Lock;
6971
import java.util.concurrent.locks.ReentrantLock;
@@ -77,6 +79,7 @@ public class DefaultClusterRenderer<T extends ClusterItem> implements ClusterRen
7779
private final ClusterManager<T> mClusterManager;
7880
private final float mDensity;
7981
private boolean mAnimate;
82+
private final Executor mExecutor = Executors.newSingleThreadExecutor();
8083

8184
private static final int[] BUCKETS = {10, 20, 50, 100, 200, 500, 1000};
8285
private ShapeDrawable mColoredCircleBackground;
@@ -290,7 +293,7 @@ public void run() {
290293
});
291294
renderTask.setProjection(projection);
292295
renderTask.setMapZoom(mMap.getCameraPosition().zoom);
293-
new Thread(renderTask).start();
296+
mExecutor.execute(renderTask);
294297
}
295298

296299
public void queue(Set<? extends Cluster<T>> clusters) {

0 commit comments

Comments
 (0)