0

With a class that has synchronized methods, we know that when a thread enters such methods, it updates its cache with the object state from main memory, and when it leaves such methods, it writes its changes to the object state in main memory.

However, in the absence of synchronized methods, will a thread ever go to main memory to get that object's state (except of course for the very first time), or write the changed state back to memory? If yes, under what conditions?

One obvious thought that comes to mind is when the cache is 'full' and dirty, the thread will have to flush it to main memory before reading in more data, or if the cache is invalidated, it will have to discard it and get latest data from main memory. Is this correct? Are there any other conditions under which such 'automatic' memory synchronization will occur?

5
  • 1
    Look up "Java 5 Memory Model" for starters. Commented Nov 12, 2012 at 4:06
  • 1
    docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.4 Enjoy! Commented Nov 12, 2012 at 4:23
  • @Hot Licks it's exactly the kind of enjoyment I'm trying to avoid. :) Commented Nov 12, 2012 at 4:28
  • 1
    Sometimes there are no shortcuts. Commented Nov 12, 2012 at 4:38
  • 2
    volatile references almost certainly qualify as an answer here. Commented Nov 12, 2012 at 5:17

1 Answer 1

1

In traditional O/S (linux, windows), threads are often interrupted and put off the processor, to let other threads to run. When such a threads switching occur, processor cache is also cleared.

Another case is volatile variables, which are simply not cached at all.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.