android Bundle put不进去 -- ArrayIndexOutOfBoundsException
程序员文章站
2022-04-01 12:19:11
android Bundle put 报数组越界11-11 06:19:36.419 1886 1910 E AndroidRuntime: java.lang.ArrayIndexOutOfBoundsException: length=0; index=011-11 06:19:36.419 1886 1910 E AndroidRuntime: at android.util.ArrayMap.put(ArrayMap.java:515)11-11 06:19:36.419 1...
android Bundle put 报数组越界
11-11 06:19:36.419 1886 1910 E AndroidRuntime: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
11-11 06:19:36.419 1886 1910 E AndroidRuntime: at android.util.ArrayMap.put(ArrayMap.java:515)
11-11 06:19:36.419 1886 1910 E AndroidRuntime: at android.os.BaseBundle.putInt(BaseBundle.java:455)
11-11 06:19:36.419 1886 1910 E AndroidRuntime: at com.android.server.location.GnssLocationProvider.updateStatus(GnssLocationProvider.java:1214)
base/services/core/java/com/android/server/location/GnssLocationProvider.java
private Location mLocation = new Location(LocationManager.GPS_PROVIDER);
private Bundle mLocationExtras = new Bundle();
private void updateStatus(int status, int svCount) {
mLocationExtras.putInt("satellites", svCount);
}
public int getStatus(Bundle extras) {
if (extras != null) {
extras.putInt("satellites", mSvCount);
}
return mStatus;
}
private void reportLocation(
{
...
mLocation.setExtras(mLocationExtras);
}
public GnssLocationProvider(){
mLocation.setExtras(mLocationExtras);
}
Location.java
private Bundle mExtras = null;
public void setExtras(Bundle extras) {
Extras = (extras == null) ? null : new Bundle(extras);
}
Bundle
base/core/java/android/os/Bundle.java
public Bundle() {
super();
mFlags = FLAG_HAS_FDS_KNOWN | FLAG_ALLOW_FDS;
}
public Bundle(Bundle b) {
super(b);
mFlags = b.mFlags;
}
base/core/java/android/os/BaseBundle.java
ArrayMap<String, Object> mMap = null;
BaseBundle() {
this((ClassLoader) null, 0);
}
BaseBundle(BaseBundle b) {
mMap = new ArrayMap<>(b.mMap);
}
问题复现
在不同的线程中去设置同一个值,
// 线程1去执行
public void setTest(){
mBunle.putInt("hello", 10);
}
// 线程2去执行
public void setTest1(){
mBunle.putInt("hello", 20);
}
// 线程3去执行
public void setTest2(){
String mindex = "hello";
if(index > 65535){
index = 0;
mindex = "index" + mindex;
}else{
index++;
}
mindex = mindex + index;
Log.d(TAG, "mindex:" + mindex + " ," + index);
mBunle.putInt(mindex, index);
}
// 就发现很容易复现 ArrayIndexOutOfBoundsException 数组越界
分析:在setTest2中有不断的扩容,扩容的过程,有执行 setTest1 和 setTest,那么这里则需要同步
也就是在扩容中,有对内存操作
本文地址:https://blog.csdn.net/LHshooter/article/details/107579968