2

I read that JVM achieves the synchronization by copying data of shared variable from the main memory to the thread's working memory according to this link.

enter link description here

for example, there is a class like this.

class Test {
private Test2 test2 = new Test2();

public void print1() {}

public synchronized void print2() {
    test2.print();
}

}

If this Test class is executed in multiple threads and "print2" method is executed in one thread, I think that a lock on Test is acquired by one Thread and other threads have to wait until the lock is released.

Now I have a question. If a lock on Test is acquired by a Thread, does it mean that data of Test and Test2 is copied from the main memory to a thread's working memory? The reason that I am saying is that "synchronized" keyword is used at instance method level and test2 is a instance variable of Test class.

I am just trying to clarify what is copied from the main memory to a thread's working memory.

Please correct me if I am wrong.

2 Answers 2

2

In brief, all shared variables will be copied (i.e. written from cache to main memory so all threads have the same data) when you use synchronized. When you use volatile, only the one volatile variable is guaranteed to be copied.

I found this to be a helpful resource which touches on the topic.

http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html


EDIT: Regarding your comment, section 17.6 of the specification answers your question:

Let T be any thread, let V be any variable, and let L be any lock. There are certain constraints on the actions performed by T with respect to V and L:

Between a lock action by T on L and a subsequent use or store action by T on a variable V, an assign or load action on V must intervene; moreover, if it is a load action, then the read action corresponding to that load must follow the lock action, as seen by main memory. (Less formally: a lock action acts as if it flushes all variables from the thread's working memory; before use they must be assigned or loaded from main memory.)

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

2 Comments

what happens if I use synchronized block something like this instead of using "synchronized" at a method level? Object lock = new Object(); synchronized(lock) {test2.print();}
@user826323 I had to refresh my memory to be able to answer this one. See my updated answer. All shared variables will be obtained from main memory when a lock is used.
0

synchronized is only required if you have at least one a writer and a reader thread. As you are not modifying anything you don't need synchronized.

The only thing which is copied is the reference test2 which will most likely be placed in a register. This will happen whether your method is synchronized or not.

8 Comments

I know that this class is not modifying anything. This is just an example. I am just trying to find out what is copied when synchronization takes place in the JVM.
So if there is another instance variable something like String s = "test", then this 's' will be copied as well?
Well, actually there is a writer in the example. private Test2 test2 = new Test2(); performs writing operation.
@Yair, That's performed in the constructor which will be single threaded.
@user826323, s would copied if you use it. Again, using synchronized makes no difference for plain reads.
|

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.