问题背景:
android项目在迭代的过程中,有个获取定位的功能模块,一直有个异常居高不下。
尝试修改了好几版,都没有解决掉这个问题
日志如下:
01-04 19:55:57.770 24194 24194 E AndroidRuntime: FATAL EXCEPTION: main
1501-04 19:55:57.770 24194 24194 E AndroidRuntime: Process: com.**, PID: 24194
1601-04 19:55:57.770 24194 24194 E AndroidRuntime: java.util.ConcurrentModificationException
1701-04 19:55:57.770 24194 24194 E AndroidRuntime: at java.util.ArrayList$Itr.next(ArrayList.java:860)
1801-04 19:55:57.770 24194 24194 E AndroidRuntime: at aiv$b.a(LocationManager.java:124)
1901-04 19:55:57.770 24194 24194 E AndroidRuntime: at com.baidu.location.LocationClient.b(Unknown Source:66)
2001-04 19:55:57.770 24194 24194 E AndroidRuntime: at com.baidu.location.LocationClient.a(Unknown Source:0)
2101-04 19:55:57.770 24194 24194 E AndroidRuntime: at com.baidu.location.LocationClient$a.handleMessage(Unknown Source:171)
2201-04 19:55:57.770 24194 24194 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:107)
2301-04 19:55:57.770 24194 24194 E AndroidRuntime: at android.os.Looper.loop(Looper.java:227)
2401-04 19:55:57.770 24194 24194 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7668)
2501-04 19:55:57.770 24194 24194 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
2601-04 19:55:57.770 24194 24194 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
2701-04 19:55:57.770 24194 24194 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:960)
该错误日志其实很熟悉,就是 fail-fast机制(java集合(Collection)中的一种错误机制)。
场景复现:
最原始代码版本:
public final class LocationManager {
private static final class LocationManagerInstance {
private static final LocationManager INSTANCE = new LocationManager();
}
public static LocationManager getInstance() {
return LocationManagerInstance.INSTANCE;
}
private LocationManager() {
// do nothing
}
private static List<SoftReference<IBdLocationListener>> mWeakReferences = new ArrayList<>();
// mILocationListener;
private static MyLocationListener myListener = new MyLocationListener();
private LocationClient mLocationClient;
/**
* 开始申请 定位
*/
public void startLocate(IBdLocationListener locationListener, Application application) {
mLocationClient = new LocationClient(application);
if (mLocationClient != null) {
mWeakReferences.add(new SoftReference<>(locationListener));
if (locationListener != null) {
locationListener.startLocation();
}
LogUtils.i("start collect location info: thread name:" + Thread.currentThread().getName());
mLocationClient.start();
}
}
/**
* 取消注册
*
* @param listener
*/
public void unRegisterListener(IBdLocationListener listener) {
synchronized (LocationManager.class) {
if (mWeakReferences == null || mWeakReferences.isEmpty()) {
return;
}
for (int i = mWeakReferences.size() - 1; i >= 0; i--) {
SoftReference<IBdLocationListener> itemListener = mWeakReferences.get(i);
if (itemListener != null && itemListener.get() != null && itemListener.get() == listener) {
mWeakReferences.remove(i);
}
}
}
}
//BDAbstractLocationListener
public static class MyLocationListener extends BDAbstractLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
if (mWeakReferences != null) {
Iterator<SoftReference<IBdLocationListener>> iterator = mWeakReferences.iterator();
while (iterator.hasNext()) {
SoftReference<IBdLocationListener> next = iterator.next();
if (next == null || next.get() == null) {
iterator.remove();
continue;
}
if (next.get() != null) {
next.get().onReceiveLocation(location);
}
}
}
}
});
}
}
}
由于是涉及到定位sdk,猜测是多线程没加锁导致的,数据不同步。于是直接加锁尝试解决上线(本身概率性事件,复现概率不是很高)。
修改如下版本:
public final class LocationManager {
private static final class LocationManagerInstance {
private static final LocationManager INSTANCE = new LocationManager();
}
public static LocationManager getInstance() {
return LocationManagerInstance.INSTANCE;
}
private LocationManager() {
// do nothing
}
private static List<SoftReference<IBdLocationListener>> mWeakReferences = new ArrayList<>();
// mILocationListener;
private static MyLocationListener myListener = new MyLocationListener();
private LocationClient mLocationClient;
/**
* 开始申请 定位
*/
public void startLocate(IBdLocationListener locationListener, Application application) {
mLocationClient = new LocationClient(application);
synchronized (LocationManager.class) {
if (mLocationClient != null) {
mWeakReferences.add(new SoftReference<>(locationListener));
if (locationListener != null) {
locationListener.startLocation();
}
mLocationClient.start();
}
}
}
/**
* 取消注册
*
* @param listener
*/
public void unRegisterListener(IBdLocationListener listener) {
synchronized (LocationManager.class) {
if (mWeakReferences == null || mWeakReferences.isEmpty()) {
return;
}
for (int i = mWeakReferences.size() - 1; i >= 0; i--) {
SoftReference<IBdLocationListener> itemListener = mWeakReferences.get(i);
if (itemListener != null && itemListener.get() != null && itemListener.get() == listener) {
mWeakReferences.remove(i);
}
}
}
}
//BDAbstractLocationListener
public static class MyLocationListener extends BDAbstractLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
synchronized (LocationManager.class) {
if (mWeakReferences != null) {
Iterator<SoftReference<IBdLocationListener>> iterator = mWeakReferences.iterator();
while (iterator.hasNext()) {
SoftReference<IBdLocationListener> next = iterator.next();
if (next == null || next.get() == null) {
iterator.remove();
continue;
}
if (next.get() != null) {
next.get().onReceiveLocation(location);
}
}
}
}
}
}
}
一直以为是线程不同步导致的,但是发现加上锁之后还是会报这个异常。
思考:
1.难道锁对象不是同一个?(确认过确实是同一个)
2.同一个线程能同时 执行两处代码?(差点颠覆了我这么多年 理论的认知)
哎,经过网上查找资料。确实也是和自己理解的一样:
参考资料:https://www.cnblogs.com/dolphin0520/p/3933551.html
对于该异常的结论:
1.在同一个线程中 确实也会报这个错误:
测试代码如下:
public class Test {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(2);
Iterator<Integer> iterator = list.iterator();
while(iterator.hasNext()){
Integer integer = iterator.next();
if(integer==2)
list.remove(integer);
}
}
}
2.多线程中更会报这个错误:
测试代码如下:
public class Test {
private static ArrayList<String> testList = new ArrayList<>();
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
new Thread(new Runnable() {
@Override
public void run() {
for (int j = 0; j < 100000; j++) {
testList.add("" + j);
}
}
}).start();
}
for (int i = 0; i < 10; i++) {
new Thread(new Runnable() {
@Override
public void run() {
for (int j = 0; j < 10000; j++) {
for (String s : testList) {
System.out.println(s);
}
}
}
}).start();
}
}
}
思考:
通过测试以及 网上查阅资料,解决方法无非就是两种(加锁 或者使用 CopyOnWriteArrayList 线程安全的集合),
那为啥我的代码,还是报错呢???哎,苦恼....
最终结论:
经过重新进行了代码逻辑走查,发现:在遍历所有监听器对象并且调用onReceiveLocation方法的时候。该对象的回调中,又会调用unRegisterListener 方法,
查看unRegisterListener方法:发现该方法中会进行 集合的删除操作。
看到这,这不就是单线程中 产生该异常的原因嘛。我擦。。。。尼玛。。。
这种迷惑性比较强的代码,确实有点坑爹。哎,学艺不精啊。。
最终修复版本代码:
public final class LocationManager {
private static final class LocationManagerInstance {
private static final LocationManager INSTANCE = new LocationManager();
}
public static LocationManager getInstance() {
return LocationManagerInstance.INSTANCE;
}
private LocationManager() {
// do nothing
}
private static List<SoftReference<IBdLocationListener>> mWeakReferences = new ArrayList<>();
// mILocationListener;
private static MyLocationListener myListener = new MyLocationListener();
private LocationClient mLocationClient;
/**
* 开始申请 定位
*/
public void startLocate(IBdLocationListener locationListener, Application application) {
mLocationClient = new LocationClient(application);
synchronized (LocationManager.class) {
if (mLocationClient != null) {
mWeakReferences.add(new SoftReference<>(locationListener));
if (locationListener != null) {
locationListener.startLocation();
}
mLocationClient.start();
}
}
}
/**
* 取消注册
*
* @param listener
*/
public void unRegisterListener(IBdLocationListener listener) {
synchronized (LocationManager.class) {
if (mWeakReferences == null || mWeakReferences.isEmpty()) {
return;
}
for (int i = mWeakReferences.size() - 1; i >= 0; i--) {
SoftReference<IBdLocationListener> itemListener = mWeakReferences.get(i);
if (itemListener != null && itemListener.get() != null && itemListener.get() == listener) {
// 在此处不要删除,重置为null。在调用处,使用iterator 遍历的时候,删除
mWeakReferences.set(i, null);
}
}
}
}
//BDAbstractLocationListener
public static class MyLocationListener extends BDAbstractLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
synchronized (LocationManager.class) {
if (mWeakReferences != null) {
Iterator<SoftReference<IBdLocationListener>> iterator = mWeakReferences.iterator();
while (iterator.hasNext()) {
SoftReference<IBdLocationListener> next = iterator.next();
if (next == null || next.get() == null) {
iterator.remove();
continue;
}
if (next.get() != null) {
next.get().onReceiveLocation(location);
}
}
}
}
}
}
}
结论虽然是很简单,但是排查过程中确实是很痛苦的。。。
做一下开发过程中的简单记录吧,加油!!!