I assume that the interviewer wanted to implement a Java version of something equivalent to a waiting / notification mechanism, without using Object.wait () and Object.notify () directly.
For example, any of the following high-level synchronization mechanisms can be used to simulate wait / notify:
- Implementing BlockingQueue
- SynchronousQueue
- CountDownLatch
- CyclicBarrier
- Futures
, , /. /, , , , "" , .
, SynchronousQueue:
public class WaitNotify {
private final Object ITEM = new Object();
private final SynchronousQueue<Object> q = new SynchronousQueue<Object>();
public void myNotify() {
q.offer(ITEM);
}
public void myWait() throws InterruptedException {
q.take();
}
}