I have two threads. The first thread calls the setX method, the second one calls the getX method. Do I have to set the methods synchronized although i have only one writing thread? And can i also solve my thread problem with the second class and the volatile variable?
public class Test {
private int x;
public synchronized void setX(int x) {
this.x = x;
}
public synchronized int getX() {
return this.x;
}
}
public class Test2 {
private volatile int x;
public void setX(int x) {
this.x = x;
}
public int getX() {
return this.x;
}
}