I'm using Java's DelayQueue to dispatch events after a second delay. But the problem is that, under heavy load, my consumers on DelayQueue block until considerable bulk of offer() operations from another threads are gone.
Does anyone know non-blocking delay queue implementations in Java?
I think you are misunderstanding either the
DelayQueue API or the way that thread scheduling works.
If you want genuine non-blocking queue operations, then
DelayQueue already provides them. For example
poll() will immediately return either a queue entry or
null. It won't block the calling thread. The
offer(...) method is the equivalent method for inserting without blocking.
On the other hand, if you are actually saying that some threads are being "starved", then there is not much you can do about that. Java thread scheduling is not "fair":
If you have lots of threads that are runnable, no attempt is made to give each one a similar amount of run time.
If you have multiple threads waiting on a primitive lock or notify, then the scheduler won't make any attempt to pick one thread "fairly".
Thread starvation is going to be more likely if you have lots more threads than you have cores to run them.
The best solution is to design your algorithms such that it doesn't matter if threads are scheduled unfairly. It shouldn't matter; see Is a DelayQueue without fairness problematic?.
For the record, I'm not aware of a
DelayQueue replacement that advertises fair scheduling.