Design and implement a thread-safe class that allows registeration of callback methods that are executed after a user specified time interval in seconds has elapsed.
- Used java condition interface
- wait() always must be called inside a loop read more
public void start() throws InterruptedException {
long sleepFor = 0;
long lastSeen = 0;
while (true) {
lock.lock();
while (priorityQueue.size() == 0) {
condition.await();
}
if (lastSeen == priorityQueue.size()) {
condition.await(sleepFor, TimeUnit.MILLISECONDS);
}
long currentTime = System.currentTimeMillis();
while (priorityQueue.size() != 0 && currentTime >= priorityQueue.peek().getExecuteAt()) {
CallBack callBack = priorityQueue.poll();
System.out.println("Executed at " + System.currentTimeMillis() / 1000
+ " required at " + callBack.getExecuteAt() / 1000
+ " message " + callBack.getMessage());
}
sleepFor = priorityQueue.size() == 0 ? 0 : priorityQueue.peek().getExecuteAt() - currentTime;
lastSeen = priorityQueue.size();
lock.unlock();
}
}