Skip to content

Commit ace4909

Browse files
authored
fix: Revert "Use thread pools (#601)" (#665)
This reverts commit 6619f14. It seems that when a thread pool is used it allows a thread to try and unlock() a lock that it did not lock(), which ReentrantLock does not allow - "If the current thread is not the holder of this lock then IllegalMonitorStateException is thrown." Fixes #660
1 parent 7a9bd54 commit ace4909

File tree

2 files changed

+26
-29
lines changed

2 files changed

+26
-29
lines changed

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@
1616

1717
package com.google.maps.android.clustering.algo;
1818

19+
import androidx.collection.LruCache;
20+
1921
import com.google.maps.android.clustering.Cluster;
2022
import com.google.maps.android.clustering.ClusterItem;
2123

2224
import java.util.Collection;
2325
import java.util.Set;
24-
import java.util.concurrent.Executor;
25-
import java.util.concurrent.Executors;
2626
import java.util.concurrent.locks.ReadWriteLock;
2727
import java.util.concurrent.locks.ReentrantReadWriteLock;
2828

29-
import androidx.collection.LruCache;
30-
3129
/**
3230
* Optimistically fetch clusters for adjacent zoom levels, caching them as necessary.
3331
*/
@@ -37,7 +35,6 @@ public class PreCachingAlgorithmDecorator<T extends ClusterItem> extends Abstrac
3735
// TODO: evaluate maxSize parameter for LruCache.
3836
private final LruCache<Integer, Set<? extends Cluster<T>>> mCache = new LruCache<Integer, Set<? extends Cluster<T>>>(5);
3937
private final ReadWriteLock mCacheLock = new ReentrantReadWriteLock();
40-
private final Executor mExecutor = Executors.newCachedThreadPool();
4138

4239
public PreCachingAlgorithmDecorator(Algorithm<T> algorithm) {
4340
mAlgorithm = algorithm;
@@ -104,10 +101,12 @@ public Set<? extends Cluster<T>> getClusters(float zoom) {
104101
Set<? extends Cluster<T>> results = getClustersInternal(discreteZoom);
105102
// TODO: Check if requests are already in-flight.
106103
if (mCache.get(discreteZoom + 1) == null) {
107-
mExecutor.execute(new PrecacheRunnable(discreteZoom + 1));
104+
// It seems this cannot use a thread pool due to thread locking issues (#660)
105+
new Thread(new PrecacheRunnable(discreteZoom + 1)).start();
108106
}
109107
if (mCache.get(discreteZoom - 1) == null) {
110-
mExecutor.execute(new PrecacheRunnable(discreteZoom - 1));
108+
// It seems this cannot use a thread pool due to thread locking issues (#660)
109+
new Thread(new PrecacheRunnable(discreteZoom - 1)).start();
111110
}
112111
return results;
113112
}

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

+20-22
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,6 @@
1616

1717
package com.google.maps.android.clustering.view;
1818

19-
import com.google.android.gms.maps.GoogleMap;
20-
import com.google.android.gms.maps.Projection;
21-
import com.google.android.gms.maps.model.BitmapDescriptor;
22-
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
23-
import com.google.android.gms.maps.model.LatLng;
24-
import com.google.android.gms.maps.model.LatLngBounds;
25-
import com.google.android.gms.maps.model.Marker;
26-
import com.google.android.gms.maps.model.MarkerOptions;
27-
import com.google.maps.android.R;
28-
import com.google.maps.android.clustering.Cluster;
29-
import com.google.maps.android.clustering.ClusterItem;
30-
import com.google.maps.android.clustering.ClusterManager;
31-
import com.google.maps.android.collections.MarkerManager;
32-
import com.google.maps.android.geometry.Point;
33-
import com.google.maps.android.projection.SphericalMercatorProjection;
34-
import com.google.maps.android.ui.IconGenerator;
35-
import com.google.maps.android.ui.SquareTextView;
36-
3719
import android.animation.Animator;
3820
import android.animation.AnimatorListenerAdapter;
3921
import android.animation.TimeInterpolator;
@@ -55,6 +37,24 @@
5537
import android.view.ViewGroup;
5638
import android.view.animation.DecelerateInterpolator;
5739

40+
import com.google.android.gms.maps.GoogleMap;
41+
import com.google.android.gms.maps.Projection;
42+
import com.google.android.gms.maps.model.BitmapDescriptor;
43+
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
44+
import com.google.android.gms.maps.model.LatLng;
45+
import com.google.android.gms.maps.model.LatLngBounds;
46+
import com.google.android.gms.maps.model.Marker;
47+
import com.google.android.gms.maps.model.MarkerOptions;
48+
import com.google.maps.android.R;
49+
import com.google.maps.android.clustering.Cluster;
50+
import com.google.maps.android.clustering.ClusterItem;
51+
import com.google.maps.android.clustering.ClusterManager;
52+
import com.google.maps.android.collections.MarkerManager;
53+
import com.google.maps.android.geometry.Point;
54+
import com.google.maps.android.projection.SphericalMercatorProjection;
55+
import com.google.maps.android.ui.IconGenerator;
56+
import com.google.maps.android.ui.SquareTextView;
57+
5858
import java.util.ArrayList;
5959
import java.util.Collections;
6060
import java.util.HashMap;
@@ -64,8 +64,6 @@
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;
6967
import java.util.concurrent.locks.Condition;
7068
import java.util.concurrent.locks.Lock;
7169
import java.util.concurrent.locks.ReentrantLock;
@@ -79,7 +77,6 @@ public class DefaultClusterRenderer<T extends ClusterItem> implements ClusterRen
7977
private final ClusterManager<T> mClusterManager;
8078
private final float mDensity;
8179
private boolean mAnimate;
82-
private final Executor mExecutor = Executors.newSingleThreadExecutor();
8380

8481
private static final int[] BUCKETS = {10, 20, 50, 100, 200, 500, 1000};
8582
private ShapeDrawable mColoredCircleBackground;
@@ -292,7 +289,8 @@ public void run() {
292289
});
293290
renderTask.setProjection(projection);
294291
renderTask.setMapZoom(mMap.getCameraPosition().zoom);
295-
mExecutor.execute(renderTask);
292+
// It seems this cannot use a thread pool due to thread locking issues (#660)
293+
new Thread(renderTask).start();
296294
}
297295

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

0 commit comments

Comments
 (0)