this class can be used in a multi-threaded thread because it is thread safe.
public class Hello {
private int value = 0;
public synchronized int get() {
return value;
}
public synchronized void set(int value) {
this.value = value;
}
}
I know that the reason we should use synchronization when get () other than set () is memory visibility.
and the java volatile keyword can be used to make memory visible.
so .. is this class also thread safe ??
public class Hello {
private volatile int value = 0;
public int get() {
return value;
}
public synchronized void set(int value) {
this.value = value;
}
}
source
share