class SyncTest {
private int x;
private int y;
private synchronized void setX(int i) {
x = i;
}
private synchronized void setY(int i) {
y = i;
}
public void setXY(int i, long sleep) {
setX(i);
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
setY(i);
}
public synchronized boolean check() {
return x != y;
}
}
public class ThreadTest extends Thread {
private SyncTest st = null;
private long sleep;
public ThreadTest(SyncTest st, long sleep) {
this.st = st;
this.sleep = sleep;
}
public void set(int i) {
st.setXY(i, sleep);
System.out.println(st.check() + " " + getName());
}
public void run() {
for (int i = 0; i < 10; i++) {
set(i);
}
}
/**
* @param args
*/
public static void main(String[] args) {
SyncTest st = new SyncTest();
ThreadTest tt = new ThreadTest(st, 1000);
ThreadTest tt1 = new ThreadTest(st, 500);
ThreadTest tt2 = new ThreadTest(st, 100);
tt.start();
tt1.start();
tt2.start();
}
}
This is what I ran and concluded for answer B