Assume my application has only one thread and the platform on which the application runs has several CPUs. It is possible that the single thread of application at some point of time executes on cpu A , reads the value of property X, modifies its value and the modified value of property X is not committed to main memory, it just resides on cpu registry or cache. Then after some time the same thread of application can continue its flow on cpu B, read the value of property X and get the stale value from main memory, as the updated value resides on cpu A registry/cache.
So till now i saw everywhere that such kind of visibility problems occur only in multithreaded apps but seems that in case i have described that is possible as well.
Any idea ?
The Java language definition says there is a "happens before" relationship between anything that happens earlier in a thread and anything that happens later in the same thread. This means that it is the responsibility of the JVM, working with the operating system, to ensure visibility of any data written earlier in the thread to any operations later in the same thread. On most operating systems, I believe this would be handled at the operating system level, by saving register state and flushing cache to memory when a thread is switched out. In any case, you don't have to worry about it when writing Java code.