简体   繁体   English

我需要同步 writeObject() 吗?

[英]Do I need to synchronize writeObject()?

I have some objects which are accessed concurrently by RMI serialization.我有一些通过 RMI 序列化并发访问的对象。 Recently I had written custom serialization methods:最近我写了自定义序列化方法:

/** This method is made to omit serialization of this.order */
private void writeObject(java.io.ObjectOutputStream out)
  throws java.io.IOException
{
    Order tmpOrder = this.order;
    this.order = null;
    out.defaultWriteObject();
    this.order = tmpOrder;
}

private void readObject(java.io.ObjectInputStream in)
  throws java.io.IOException, ClassNotFoundException
{
    in.defaultReadObject();
}

I don't want to permit to spoil this.order by concurrent RMI thread.我不想允许并发 RMI 线程破坏 this.order。

  1. Do I need to make writeObject synchronized?我需要使 writeObject 同步吗? or要么
  2. Does the RMI framework do all the best to synchronize the access to the object? RMI 框架是否尽力同步对 object 的访问?

In second case my synchronization could even cause deadlocks in RMI.在第二种情况下,我的同步甚至可能导致 RMI 中的死锁。 The general contract of JAVA API is that a method is called by one thread except noted specifically. JAVA API 的总契约是一个方法被一个线程调用,除非特别注明。 So if I should follow the rule I should leave the writeObject w/o any synchronization.因此,如果我应该遵循规则,我应该让writeObject不带任何同步。 Is this correct?这个对吗?

Another solution to my problem without answering the question is certainly declaring private static final ObjectStreamField[] serialPersistentFields .另一个没有回答问题的解决方案肯定是声明private static final ObjectStreamField[] serialPersistentFields (I cannot make a field transient because the object is not only a DTO but an JPA Entity also) (我不能使字段瞬态,因为 object 不仅是 DTO,而且还是 JPA 实体)

I have some objects which are accessed concurrently by RMI serialization我有一些通过 RMI 序列化同时访问的对象

No you don't.不,你不知道。 You have some objects that are accessed concurrently by Object Serialization.您有一些对象被Object 序列化并发访问。

Does the RMI framework do all the best to synchronize the access to the object? RMI 框架是否尽力同步对 object 的访问?

No. The Object Serialization framework might, but it isn't specified.否。Object 序列化框架可能会,但未指定。

Attempting to serialise objects that are being used concurrently is going to lead to a mess.尝试序列化同时使用的对象将导致混乱。 Certainly to read a consistent state or even a well-formed state you will generally need exclusive locking.当然,要读取一致的 state 甚至格式正确的 state,您通常需要独占锁定。 If writeObject is syncrhonised, then you have extreme problems making sure the lock-ordering is well behaved.如果writeObject是同步的,那么在确保锁顺序表现良好时会遇到极端问题。

Adding serialPersistentFields (spelt correctly:) should have the same behaviour as making fields transient .添加serialPersistentFields (拼写正确:)应该与将字段设置为transient具有相同的行为。 Making order transient will stop it being written out, which seems to be what you are attempting in the question code.使order transient将阻止它被写出,这似乎是您在问题代码中尝试的。 Using ObjectOutputStream.putFields can also achieve something similar.使用ObjectOutputStream.putFields也可以达到类似的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM