Java. Serialization of objects in a multithreaded environment(爪哇.多线程环境中对象的序列化)
问题描述
我有一个对象,其内部可变状态由一个或多个线程不断更新.对象是同步的,目标是从另一个线程定期保存其状态(通过序列化):
I have an object whose internal mutable state is being constantly updated by one or more threads. The object is synchronized, and the goal is to periodically save its state (via serialization) from yet another thread:
public class Counter implements Serializable {
private int dogCount;
private int catCount;
public synchronized void updateFromDogThread( int count ) {
dogCount = count;
}
public synchronized void updateFromCatThread( int count ) {
catCount = count;
}
}
问题:
- 在这种情况下,序列化安全吗?
- 它在后台是如何工作的?也就是说,
ObjectOutputStream
会执行序列化块,直到没有线程在Counter
上运行? - 如果
Counter
的同步不使用内部锁,而是使用其他锁怎么办?
- Is serialization safe in this case?
- How does it work under the hood? That is to say, will the
ObjectOutputStream
performing the serialization block until no threads are any longer operating onCounter
? - What if
Counter
's synchronization doesn't use the intrinsic lock, but some other lock?
推荐答案
- 在这种情况下,序列化安全吗?
没有.正如@Tom Hawtin 所说,您需要执行自己的锁定以确保在序列化对象时不会更改对象.
No. As @Tom Hawtin says, you will need to perform your own locking to ensure that the object(s) are not changed while you are serializing them.
- 它在后台是如何工作的?也就是说,ObjectOutputStream 是否会执行序列化阻塞,直到没有线程在 Counter 上运行?
ObjectOutputStream
在底层没有锁定.如有必要,由应用程序执行此操作.
ObjectOutputStream
does no locking under the hood. It is up to the application to do this, if it is necessary.
- 如果 Counter 的同步不使用内部锁,而是使用其他锁怎么办?
那么您的应用程序还需要在序列化发生时使用其他锁来锁定更新.
Then your application will also need to use that other lock to lock out updates while serialization is happening.
如果您要序列化的状态仅由一个具有两个字段的对象的状态组成,那么锁争用和粒度应该不是问题.但是如果对象很复杂,那么锁争用很可能是有问题的,获取锁而不冒死锁风险的问题也是如此.这种情况需要仔细设计.
If the state that you are serializing simply consists of the state of one object with two fields, then lock contention and granularity should not be a problem. But if the object(s) are complicated, then lock contention could well be problematic, as could the problem of acquiring the locks without risking deadlock. That scenario would require careful design.
这篇关于爪哇.多线程环境中对象的序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:爪哇.多线程环境中对象的序列化
基础教程推荐
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01