Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does java remove/optimize unnecessary synchronized statements?

Let's imagine someone synchronizes a method returning an int:

int whatever = 33;
...

public synchronized int getWathever() {
    return this.whatever;
}

We know from Java specs that ints are modified atomically. Hence, the synchronized statement is not necessary.

Will the compilers remove/optimize it?

like image 913
Jérôme Verstrynge Avatar asked Aug 13 '11 02:08

Jérôme Verstrynge


People also ask

Why is Synchronisation necessary in Java?

Synchronization in java is the capability to control the access of multiple threads to any shared resource. In the Multithreading concept, multiple threads try to access the shared resources at a time to produce inconsistent results. The synchronization is necessary for reliable communication between threads.

Why are locks better than synchronized?

Only one thread is allowed to access only one method at any given point of time using a synchronized block. This is a very expensive operation. Locks avoid this by allowing the configuration of various locks for different purpose.

What are the important methods that are used in Java synchronization?

It can be achieved by using the following three ways: By Using Synchronized Method. By Using Synchronized Block. By Using Static Synchronization.

How do I stop synchronization in Java?

Method 1 modifying the variable a and method 2 modifying the variable b, the concurrent modification of the same variable by two threads should be avoided and it is. BUT while thread1 modifying a and thread2 modifying b it can be performed without any race condition.


2 Answers

No, that simply is not possible. If the compiler and the JVM were to do so, it is likely that the constraints set by the Java programming language memory model would be violated.

More specifically, the synchronization order order stated in the Java Language Specification would be violated. If a compiler or a JVM* were to remove any "unwanted" synchronizations, then any further optimizations performed would violate any assumptions placed by a developer on the synchronization order (and the happens-before) relationship. In your specific case, any write to the integer will happen before the read, in a compiler/JVM that obeys the Java memory model.

A compiler/JVM that removes the synchronizations would simply result in an environment where the memory model is violated. For example, method in-lining could be performed without the compiler/JVM placing a memory barrier before the read of the integer value, thereby allowing for stale values to be read from a cached value.

* Note, the reference to the compiler/JVM duo is intentional. A compiler will only emit bytecode that complies with the JLS; a JVM could simply have a bug where the requirements of the memory model could still be violated. For the sake of completeness of the memory model, both the compiler and the JVM should comply with the requirements set by the memory model.

like image 143
Vineet Reynolds Avatar answered Sep 28 '22 06:09

Vineet Reynolds


synchronized has other thread safety effects besides getting a lock (ensuring the modified data is visible to other threads for one)

as long as these side effects are valid the JIT is essentially free to do what it wants

though in the example it has to ensure the lock is not held by any other thread during the loading of the variable which is easiest to ensure by effectively getting the lock

like image 33
ratchet freak Avatar answered Sep 28 '22 05:09

ratchet freak