Search code examples
javaserializationthread-safetyrmiconcurrenthashmap

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?


Solution

  • 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.