在java中序列化:自动线程安全? [英] Serializing in java: automatic thread-safety?

查看:146
本文介绍了在java中序列化:自动线程安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你在Java中序列化一个对象并将它(通过套接字)发送到集群中的节点,你会自动获得线程安全吗?

If you serialize an object in Java and send it (over a socket) to nodes in a cluster do you automatically get thread safety?

假设您有一个群集,每个节点有多个核心。服务器有一个Java对象,它希望发送到每个集群上的每个核心进行处理。

Say you have a cluster and each node has several cores. The server has a Java Object that it wants to send to each core on each cluster to process. It serializes that object and sends it to each receiver.

通过序列化,这个对象是否自动地被深度复制,你是否自动在该对象上获得线程安全?你不会在集群上的各个节点之间遇到任何并发问题,因为他们不能访问内存中的同一个地方...但是节点上的核心之间呢?

Through serializing, is that object automatically somewhat "deep copied" and do you automatically get thread safety on that object? You aren't going to get any concurrency problems between the various nodes on the cluster because they can't be accessing the same place in memory... but what about between cores on the nodes?

推荐答案

序列化操作不是线程安全的,它不是一个好主意serilaize正在被修改的对象。但是,在同一个线程,进程或Universe中对象的任何深拷贝都是完整的副本,在更改原始副本时不会更改。

The serialization operation is not thread safe and it not a good idea to serilaize an object which is being modified. However any deep copy of the object, in the same thread, process or universe is a complete copy and is not changed when you change the original.

注意:如果发送再次更新对象,您可能不会看到更新,因为它只是发送对对象的引用。因此,如果您要发送更新,则应在发件人上调用 reset()。使用 reset()的另一个原因是避免内存泄漏,因为发送方和接收方会记住他们曾经写/读的每个对象。

Note: if you send the updated object again, you may not see the updates because it just sends a reference to the object. For this reason you should call reset() on the sender if you want to send an update. Another reason to use reset() is to avoid a "memory leak" as the sender and receiver will otherwise remember every object they ever wrote/read.

这篇关于在java中序列化:自动线程安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆