Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need of synchronization in getters and setters

Probably a very silly question. Just wanted to confirm my understanding.

class Test
{
       private volatile String id;

       public void setID(String id)
       {
             this.id = id;
       }

       public String getID()
       {
             return id;
       }
}

Lets say that an object of above class can be accessed by multiple threads. My understanding is that in case of simple getter and setters like above (doing simple initialization), I do not need to make these methods synchronized, correct ?
I guess volatile is needed as otherwise value of id can be outdated otherwise in different threads.
So, can anyone see any problem if we do not have these methods as synchronized ?

like image 413
snow_leopard Avatar asked Dec 11 '13 10:12

snow_leopard


People also ask

What is the purpose to use synchronization?

The main purpose of synchronization is the sharing of resources without interference using mutual exclusion. The other purpose is the coordination of the process interactions in an operating system. Semaphores and monitors are the most powerful and most commonly used mechanisms to solve synchronization problems.

Why do we need to synchronize threads?

Thread synchronization is the concurrent execution of two or more threads that share critical resources. Threads should be synchronized to avoid critical resource use conflicts. Otherwise, conflicts may arise when parallel-running threads attempt to modify a common variable at the same time.

Does it requires to synchronize volatile getter and setter methods?

making the double field volatile would be satisfying if you are using Java 1.5 or bigger and use only set and get ( setValue does not have to be synchronized then). See java. util.

Why synchronized block is required?

A synchronized block ensures that a call to a method that is a member of an object occurs only after the current thread has successfully entered the object's monitor.


1 Answers

My understanding is that in case of simple getter and setters like above (doing simple initialization), I do not need to make these methods synchronized, correct ?

Correct, because what they get and set (an object reference) is treated atomically by the JVM.

The answer would be "No, you do need synchronization" if you were using a long or double and you didn't have it marked volatile.

Both aspects of this are covered in the JLS, §17.7:

17.7. Non-atomic Treatment of double and long

For the purposes of the Java programming language memory model, a single write to a non-volatile long or double value is treated as two separate writes: one to each 32-bit half. This can result in a situation where a thread sees the first 32 bits of a 64-bit value from one write, and the second 32 bits from another write.

Writes and reads of volatile long and double values are always atomic.

Writes to and reads of references are always atomic, regardless of whether they are implemented as 32-bit or 64-bit values.

like image 185
T.J. Crowder Avatar answered Sep 20 '22 14:09

T.J. Crowder