Does Java sync update the full cache, or only my synchronized objects?

If I access objects in a synchronization method or synchronization block, are all objects in the accessed element synchronized?

Imagine an object queue with synchronous add () and take () methods that accept and emit complex object thing There are many lists of things with different objects

Now before creating something, the image thread puts some existing objects into something, modifies some of them, and so on Before thread added something to queue Later, the thread grabs things from the queue

Question: put thing and all its children / sub objects in the same state as before leaving? Even if the thread may work earlier in one of these child elements? Because I image the thread, the processor may still have some cache information on the sub element (the address of the sub object is still the same) All these cached things are invalid only by accessing the parent object thing synchronously?

Please do not give answers such as using concurrent libraries I want to know what happened

Solution

An important concept in the JAVA memory model is happens before order The results of write operations that occur before read operations are visible to these read operations Other results may or may not be visible

The sequence of events is caused by the synchronous sequence of actions between threads and the natural sequence of actions in a single thread

If you synchronize before an object (such as your queue), and do all operations on thing and its "child objects" within or before this synchronization block, and synchronize in the same queue and read these object synchronization blocks after this object, all these changes can be seen after

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>