java在同一进程中反序列化和序列化

时间:2013-10-23 11:46:38

标签: java serialization deserialization

我在同一个JVM中序列化和反序列化对象列表时遇到问题。确切地说,现在我的对象对Alphabet对象具有相同的引用,该对象具有以下规则:

    VMID instanceId = new VMID();  //used in readResolve to identify persitent instances

    public Alphabet (int capacity, Class entryClass) {
        this.map = new gnu.trove.TObjectIntHashMap (capacity);
        this.entries = new ArrayList (capacity);
        this.entryClass = entryClass;
        // someone could try to deserialize us into this image (e.g., by RMI).  Handle this.
        deserializedEntries.put (instanceId, this);
    }

    public VMID getInstanceId() {
        return instanceId;
    } // for debugging

    public void setInstanceId(VMID id) { this.instanceId = id; }

    // Serialization
    private static final long serialVersionUID = 1;

    private static final int CURRENT_SERIAL_VERSION = 1;

    private void writeObject (ObjectOutputStream out) throws IOException {
        out.writeInt (CURRENT_SERIAL_VERSION);
        out.writeInt (entries.size());
        for (int i = 0; i < entries.size(); i++) {
            out.writeObject (entries.get(i));
        }
        out.writeBoolean (growthStopped);
        out.writeObject (entryClass);
        out.writeObject(instanceId);
    }

    private void readObject (ObjectInputStream in) throws IOException, ClassNotFoundException {
        int version = in.readInt ();
        int size = in.readInt();
        entries = new ArrayList (size);
        map = new gnu.trove.TObjectIntHashMap (size);
        for (int i = 0; i < size; i++) {
            Object o = in.readObject();
            map.put (o, i);
            entries. add (o);
        }
        growthStopped = in.readBoolean();
        entryClass = (Class) in.readObject();
        if (version >0 ){ // instanced id added in version 1S
            instanceId = (VMID) in.readObject();
        }
    }

    private transient static HashMap deserializedEntries = new HashMap();
    /**
     * This gets called after readObject; it lets the object decide whether
     * to return itself or return a previously read in version.
     * We use a hashMap of instanceIds to determine if we have already read
     * in this object.
     * @return
     * @throws ObjectStreamException
     */

    public Object readResolve() throws ObjectStreamException {
        Object previous = deserializedEntries.get(instanceId);
        if (previous != null){
            //System.out.println(" ***Alphabet ReadResolve:Resolving to previous instance. instance id= " + instanceId);
            return previous;
        }
        if (instanceId != null){
            deserializedEntries.put(instanceId, this);
        }
        //System.out.println(" *** Alphabet ReadResolve: new instance. instance id= " + instanceId);
        return this;
    }

现在我的对象列表反序列化,在某些时候Alphabet引用不匹配。我使用以下方法进行了检查:

for (Instance i: finalTrainingDocs){
    if (!i.getTargetAlphabet().equals(finalTraining.getTargetAlphabet())){
        System.out.println("not equals");
        System.out.println(i.getTargetAlphabet().getInstanceId() + " " + finalTraining.getTargetAlphabet().getInstanceId());
    }
    finalTraining.add(i);
    counter++;
    System.out.println("counter " + counter);
}

并取回以下结果

counter 237
counter 238
counter 239
not equals
3ce62156867eb540:6b7f0de5:141e51fcd67:-7ffa 3ce62156867eb540:6b7f0de5:141e51fcd67:-7ffa

现在看一下VMId,因为它们是相同的,不应该是同一个对象,就像上面的逻辑一样?谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

一种可能性是你有竞争条件;即两个线程同时更新deserializedEntries散列映射。这可能会导致您有两个Alphabet个实例,其值相等instanceId

deserializedEntries声明为volatile并不足以防止这种情况发生。 (事实上​​,你的同步不足甚至可能导致hashmap的内部数据结构被破坏。)


我不相信你所做的是一个好主意。除了这种脆弱性(需要更多的重量级同步来修复)之外,你还遇到了hashmap是内存泄漏的问题。我怀疑通过接受Alphabet实例重复,并覆盖equals来处理此问题,您将获得更好的性能。

答案 1 :(得分:-1)

您正在根据版本

阅读instanceId
if (version >0 ){ // instanced id added in version 1S
    instanceId = (VMID) in.readObject();
}

所以同样的条件需要在这里适用

 if (CURRENT_SERIAL_VERSION >0 ){ 
      out.writeObject(instanceId);