Java forced volatile access
Consider such a situation
There are two threads and a shared resource (such as HashMap) A thread creates a HashMap and initializes it with some key value pairs, and after initializing the shared resource, it will never be modified again
Now, the second thread is strictly created after initializing the shared resource and wants to use it At this point, I want to ensure that the second thread will use the correct version of the shared resource I assume that the first thread may not refresh the changes to main memory before creating the second thread, so the second thread brings the old value of the shared resource into its cache
Whether this analysis is correct, and how to manually force flushing to the main memory in Java after initializing the shared resources, just as I do not want or need volatile or synchronization
Solution
The documentation says:
Therefore, if your code matches your description, it is safe