1

From the book Java in Nutshell 6th edition one can read:

The reason we use the word synchronized as the keyword for “requires temporary exclusive access” is that in addition to acquiring the monitor, the JVM also rereads the current state of the object from the main memory when the block is entered. Similarly, when the synchronized block or method is exited, the JVM flushes any modified state of the object back to the main memory.

as well as:

Without synchronization, different CPU cores in the system may not see the same view of memory and memory inconsistencies can damage the state of a running the program, as we saw in our ATM example.

It suggests when the synchronized method is entered the object is loaded from the main memory to maintain memory consistency

But is this the case for objects without synchronized keywords also? So in case of a normal object is modified in one core of a CPU is synchronized with main memory so that other cores can see it?

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
Shailesh
  • 405
  • 1
  • 5
  • 18

2 Answers2

1

First, similar to what happen with other miss information going around like:

Volatile is supposed to make the threads read the values from RAM disabling thread cache

More detail about why that is not the case can be found this SO thread.

That can be applied to the statements:

the JVM also rereads the current state of the object from the main memory when the block is entered

and

when the synchronized block or method is exited, the JVM flushes any modified state of the object back to the main memory

Citing David Schwarz that kindly pointed out the following in the comments and allowed me to used:

That does not happen on any modern system. These are things that the platform might, in theory, have to do to make synchronized work but if they're not necessary on the platform (and they aren't on any platform you're likely to use), they aren't done. These statements are all in regard to a hypothetical system that has no hardware synchronization and might require these steps. Actual systems have very different hardware designs from this simplified hypothetical system and require very different things to be done. (For example, they typically just require optimization and memory barriers, not flushes to main memory or reads. This is because modern systems are optimized and use caches to avoid having to flush to or re-read from main memory because main memory is very slow and so modern CPUs have hardware optimizations to avoid it.)


Now going back to your question:

But this is case for object without synchronized keyword also ? So in case of normal object is modified in one core of CPU is synchronized with main memory so that other core can see it?

TL;DR: It might or not happen; it depends on the hardware and if the Object is read from cache; However, with the use of the synchronized the JVM ensures that it will be.


More detailed answer

So in case of the normal object is modified in one core of CPU is synchronized with main memory so that other core can see it?

To keep simple and concise, without synchronized it depends on the hardware architecture (e.g., Cache protocol) where the code will be executed and it depends if the Object is (or not) in the cache when it is read/updated.

If the architecture forces that the data in the cores is always consistence with the other cores, then yes. Accessing the cache is much faster than accessing the main memory, and accessing the first levels of cache (e.g., L1) is also faster than access the other levels.

Hence, for performance reasons, normally when the data (e.g., an Object) is loaded from main memory it gets stored in the cache (e.g., L1, L2, and L3) for quicker access in case that same data is needed again.

The first levels of cache tend to be private to each core. Therefore, it might happen that different cores have stored in their private cache (e.g., L1) different states of the "same Object". Consequently, Threads might also be reading/updating different states of the "same Object".

Notice that I wrote "same Object" because conceptually it is the same Object but in practice it is not the same entity but rather a copy of the same Object that was read from the main memory.

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
1

While the other answer talks about the importance of cache synchronisation and the memory hierarchy, the synchronized keyword doesn’t control the state of the object as a whole; rather, it is about the lock associated with that object.

Every object instance in Java can have an associated lock which prevents multiple threads running at the same time for those blocks which are synchronized on the lock. This is either implicit on the this instance or on the argument to the synchronized keyword (or the class if a static method).

While the JMM semantics say that this lock object is properly controlled and available in cache levels, it doesn’t necessarily mean therefore that the object as a whole is protected; fields read from different threads while a single thread is running in a synchronized block or method aren’t dealt with, for example.

In addition the Java memory model has defined “happens before” relationships about how data changes may become visible between threads that you need to take into account, which is why the “volatile” keyword and AtomicXxxx types are present, including var handles relaxed memory models.

So when you talk about synchronised, you need to be aware that it’s only shot the state of the object’s lock and not the state within the object that it is protecting.

AlBlue
  • 23,254
  • 14
  • 71
  • 91