Thread Safe Deferred Callback
Explore how to design a thread-safe deferred callback executor in Java that handles registering and executing callbacks after specified delays. Understand concurrency management using priority queues, ReentrantLocks, and condition variables, and learn to build a solution that avoids busy waiting while correctly scheduling callback execution times in a multithreaded environment.
We'll cover the following...
Problem Statement
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.
Solution
Let us try to understand the problem without thinking about concurrency. Let's say our class exposes an API called registerCallback() that'll take a parameter of type Callback, which we'll define later. Anyone calling this API should be able to specify after how many seconds should our ...