public class CountDown implements Runnable {
private static int counter = 0;
private int id = 0;
private int a, b, result;
private long time = 0;
private boolean done = false;
public CountDown(int a, int b, long time) {
this.a = a;
this.b = b;
this.time = time;
}
public CountDown() {
this.id = ++counter;
}
@Override
public void run() {
try {
TimeUnit.SECONDS.sleep(time);
this.result = a + b;
} catch (InterruptedException e) {
e.printStackTrace();
}
this.done = true;
synchronized (this) {
this.notify();
}
}
public Integer getResult() {
if (!done) {
try {
synchronized (this) {
System.out.println("Who : " + Thread.currentThread().getName());
this.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return result;
}
}