Can "CopyOnWriteArrayList" and "ConcurrentHashMap" be serialized?
I have a class teleport by using RMI.But I am not sure those thread-safe object can be serialized.Does anyone tried before?
开发者_运维百科UPDATE skaffman says yes,But I failed in serialize.
This is the class that I teleport.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package net.shisoft.beans;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
/**
*
* @author Shisoft
*/
public class WhatzNewList {
ConcurrentHashMap<String, CopyOnWriteArrayList<WhatzNewEntry>> WhatzNewTable = new ConcurrentHashMap<String, CopyOnWriteArrayList<WhatzNewEntry>>();
String user;
public ConcurrentHashMap<String, CopyOnWriteArrayList<WhatzNewEntry>> getWhatzNewTable() {
return WhatzNewTable;
}
public void setWhatzNewTable(ConcurrentHashMap<String, CopyOnWriteArrayList<WhatzNewEntry>> WhatzNewTable) {
this.WhatzNewTable = WhatzNewTable;
}
public String getUser() {
return user;
}
public void setUser(String usere) {
this.user = usere;
}
public WhatzNewList(String user) {
this.user = user;
}
}
this is the class WhatzNewEntry
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package net.shisoft.beans;
import java.util.Date;
/**
*
* @author Shisoft
*/
public class WhatzNewEntry {
String Title;
String context;
String contact;
Date Time;
public Date getTime() {
return Time;
}
public void setTime(Date Time) {
this.Time = Time;
}
public String getTitle() {
return Title;
}
public void setTitle(String Title) {
this.Title = Title;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
may be skaffman is right,But what wrong here?
They both implement java.io.Serializable
. So yes, they can be serialized.
Whether their contents can be serialized, of course, is a different question altogether.
ConcurrentHashMaps (CHMs) are heavyweight objects that are not efficiently serialized. Eg. Why would you want to serialize all the locking info?
Converting to an intermediate object like HashMap, Map, Set, or even toString() is much more efficient.
Most efficient of course is to not use default Serializable behavior at all, but rather write your own Externalizable interface which gives you total control of the buffer contents.
Eg. - here is a serialized empty CHM:
¬í sr &java.util.concurrent.ConcurrentHashMapd™Þ‡)= I segmentMaskI segmentShift[ segmentst 1[Ljava/util/concurrent/ConcurrentHashMap$Segment;xp ur 1[Ljava.util.concurrent.ConcurrentHashMap$Segment;Rw?A2›9t xp sr .java.util.concurrent.ConcurrentHashMap$Segment6LX“)= F loadFactorxr (java.util.concurrent.locks.ReentrantLockfU¨,,Èjë L synct /Ljava/util/concurrent/locks/ReentrantLock$Sync;xpsr 4java.util.concurrent.locks.ReentrantLock$NonfairSynceˆ2çS{¿ xr -java.util.concurrent.locks.ReentrantLock$Sync¸¢”ªDZ| xr 5java.util.concurrent.locks.AbstractQueuedSynchronizerfU¨Cu?Rã I statexr 6java.util.concurrent.locks.AbstractOwnableSynchronizer3߯¹mo© xp ?@ sq ~ sq ~ ?@ sq ~ sq ~ ?@ sq ~ sq ~ ?@ sq ~ sq ~ ?@ sq ~ sq ~ ?@ sq ~ sq ~ ?@ sq ~ sq ~ ?@ sq ~ sq ~ ?@ sq ~ sq ~ ?@ sq ~ sq ~ ?@ sq ~ sq ~ ?@ sq ~ sq ~ ?@ sq ~ sq ~ ?@ sq ~ sq ~ ?@ sq ~ sq ~ ?@ ppx
Gross. Here is the same CHM serialized from toString():
’ t {}
By doing a little conversion work, you get huge bandwidth savings!
What is funny is, that if I use serializing within my UnitTest Test class, and I try to serialize class (perfectly serializable) but containing CopyOnWriteArrayList this way:
public class SerializeTest {
@Test
public void Test() {
// serializeanyserializable class
}
Throws exception:
java.io.NotSerializableException: SerializeTest ... at java.util.concurrent.CopyOnWriteArrayList.writeObject(CopyOnWriteArrayList.java:857)
And when debugging, what I see: CopyOnWriteArrayList uses temporary object derived from SerializeTest, like SerializeTest$1.
So, only solution is that I have to make Test serializable, and then it suddenly starts to work.
Does anyone has explanation?
精彩评论