ConcurrentLinkedQueue:NotSerializableException

时间:2015-07-31 21:00:44

标签: java exception serialization java.util.concurrent concurrenthashmap

我需要序列化ConcurrentLinkedQueue。

public class Service implements Serializable { 
    private static Queue<Message> messages = new ConcurrentLinkedQueue<>(); 

    public class Serialize {
        public static Queue<Message> getMessages() 
        { 
            return messages; 
        } 
    }
}

尝试:

FileOutputStream fos = new FileOutputStream("filename");
ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(Service.Serialize.getMessages());

结果:

  

java.io.NotSerializableException:server.Service $ 1 at   java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)     在   java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)     在   java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:441)     在   java.util.concurrent.ConcurrentSkipListMap.writeObject(ConcurrentSkipListMap.java:1430)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:497)at   java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)     在   java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496)     在   java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)     在   java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)     在   java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)     在   java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)     在   java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)     在   java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)     在   java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)

如何???

2 个答案:

答案 0 :(得分:0)

在本课程的某个地方,你已经省略了要发布的内容,你正在使用并尝试序列化一个匿名内部类,正如异常明确指出的那样。

答案 1 :(得分:-1)

也许你应该摆脱内部类Serialize并将该方法放在Service ..