Given the below mentioned code and suppose that we have two different threads thread1,thread2 as well as two different objects p1 and p2 of the class BCell.
If thread1 executes p1.swap(p2) and thread2 executes p2.swap(p1) simultaneously what is a possible problem that may occur?
I have already read here and here but it didn't seem to help.
class BCell {
int value;
public synchronized int getValue() {
return value;
}
public synchronized void setValue(int i) {
value=i;
}
public synchronized void swap(BCell x) {
int temp = getValue();
setValue(x.getValue);
x.setValue(temp);
}
}