| Quiz | Posted | Given in class | ||
|---|---|---|---|---|
| 6 | Feb | 11 | Feb | |
Consider the following class which you are not allowed to modify
public class Num {
private int n;
public Num(int n) { setN(n); }
public void setN(int n) { this.n = n; }
public int getN() { return n; }
}
and suppose we have two instances of Num:
Num num1 = new Num(0); Num num2 = new Num(1);
num1.setN(num1.getN() + 1);
Answer: The thread executing the above statement could be interrupted between its fetch of n and its store of n; this is true of any thread even if proper locks are obtained. but with no lock obtained, other threads could execute similar statements causing race conditions at the stores and fetches of n. Thus, the view of the update of n is not atomic.
Num class. Write code to
execute the above statement atomically.
Answer:
synchronized(num1) {
num1.setN(num1.getN() + 1);
}
Num class.
Write code to swap the contents of num1 and num2
atomically.
You must be holding a lock on
both objects for this to work.
Answer:
synchronized(num1) {
synchronized(num2) {
int tmp = num1.getN();
num1.setN(num2.getN());
num2.setN(tmp);
}
}
public class GenID {
private static int nextID = 0;
private int myID;
public GenID() {
myID = nextID;
nextID = nextID + 1;
}
public void getID() { return myID; }
}
Answer:
When no constructor calls for GenID() can execute concurently.
That would not be true if multiple, unsynchronized threads could instantiate
GenID objects concurrently.
Answer:
public class GenID {
private static Object lock = new new Object(); // just for locking
private static int nextID = 0;
private int myID;
public GenID() {
myID = nextID;
synchronized (lock) {
nextID = nextID + 1;
}
}
public void getID() { return myID; }
}