Skip to content

Latest commit

 

History

History
50 lines (33 loc) · 1.56 KB

File metadata and controls

50 lines (33 loc) · 1.56 KB

Deferred Callback

Requirement

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.

Notes

Code

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();
        }
    }

return to main page