+/EE360P

distributed systems

git clone https://git.julia.blue/+/EE360P

vijaygarg1init9cde2af

master
738 B31 linesraw
1class Cell {
2    int value;
3    public synchronized int getValue() {
4        return value;
5    }
6    public synchronized void setValue(int i) {
7        value = i;
8    }
9    protected void doSwap(Cell x) {
10        int temp = getValue();
11        setValue(x.getValue());
12        x.setValue(temp);
13    }
14    public void swap(Cell x) {
15        if (this == x)
16            return;
17        if (System.identityHashCode(this)
18                < System.identityHashCode(x))
19            synchronized(this) {
20              synchronized(x) {
21                 doSwap(x);
22              }
23            }
24        else
25              synchronized(x) {
26                synchronized(this) {
27                 x.doSwap(this);
28              }
29            }
30    }
31}