ANdroid Serialization causes ConcurrentModificationException. How can I avoid this?
On serialization my object which is a custom class, holding various ArrayLists, every so often I get a Concurrent Mod Exception. Clearly one or more of the arraylists is throwing this. But I don't know where, or how to fix it. Implementing an iterator would be my first idea, but how to go about doing that for serialization?
This is my serialization code:enter code here
try{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ObjectOutput out = new ObjectOutputStream(bos);
out.writeObject(TGame);
// Get the bytes of the serialized object
byte[] buf = bos.toByteArray();
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/game_folder");
dir.mkdirs();
File file = new File(dir, "serializationtest");
FileOutputStream fos = new FileOutputStream(file);
//this.开发者_Python百科openFileOutput(filename, Context.MODE_PRIVATE);
fos.write(buf);
fos.close();
} catch(IOException ioe) {
Log.e("serializeObject", "error", ioe);
}catch(StackOverflowError e){
//do something
}
File f =this.getDir(filename, 0);
Log.v("FILE SAVED",f.getName());
}catch(ConcurrentModificationException e){
//do something
}
}
While Java api is serializing the object ( here inner array list), if at the same time some other thread Structurally Modifies ArrayList then you get a Concurrent Mod Exception.
One solution is Locking Mechanism ensuring that only one thread accesses that object at a Time. Another simple solution is that the object that is to be written, create a shallow copy of that object and serialize that copy. This way even if original ArrayList changes, shallow copy will have not effect and will work fine. e.g.
class Test {
int a;
string b;
ArrayList<String> c;
Test(Test t){
this.a=t.a;
this.b=t.b;
this.c=new ArrayList<String>(t.c);
}
}
FileOutputStream fos = new FileOutputStream(file);
//write a copy of original object
fos.write(new Test(t));
}
精彩评论