Serialization in java: automatic thread protection?

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

Say you have a cluster, and each node has multiple cores. There is a Java object on the server that it wants to send to each core of each cluster for processing. It serializes this object and sends it to each recipient.

Through serialization, this object is automatically somewhat "deeply copied", and you automatically get thread safety on this object? You will not run into concurrency problems between different cluster nodes because they cannot access the same memory location ... but what about between the kernels on the nodes?

+5
source share
3 answers

The serialization operation is not thread safe, and it is not recommended to serialize an object that is being modified. However, any deep copy of an object in the same thread, process, or universe is a full copy and does not change when the original changes.

Note. If you send the updated object again, you may not see the update because it simply sends a link to the object. For this reason, you must call the reset()sender if you want to send the update. Another reason for using it reset()is to avoid a โ€œmemory leak,โ€ since the sender and receiver would otherwise remember every object they have ever written / read.

+4
source

" ", ?

, , ...

  • . , , , (.. ) .

  • , , , , .

  • , . , , , .

concurrency , ... ?

, , . Deserialization . .

+3

This is definitely "thread safe" (in the sense that you define it). Serialization, by definition, is a deep copy of an object, and each worker will receive a different object.

+1
source

All Articles