[concurrency-interest] Frequently canceled timers

Joe Bowbeer joe.bowbeer at gmail.com
Thu Jan 24 18:19:17 EST 2008


> Richard Johns wrote:
>
> I have an application that retransmits messages when they don't get a
> timely response . Usually, of course, that's not necessary and their
> timers are canceled. This is a traffic generator so message rate is
> high and we need to remove the canceled timers.
>
> Is there a timer implementation that efficiently cleans up canceled
> timers? DelayQueue uses a binary heap so it's very fast otherwise, but
> it uses a sequential search for remove(). ScheduledThreadPoolExecutor
> uses DelayQueue, and Timer also uses a binary heap.
>
> Joe Bowbeer wrote:
>
> It should be more efficient to call executor.purge() periodically than
> to call remove() frequently. Will this work in your case?
>
> Richard Johns wrote:
>
> No, ThreadPoolExecutor.purge() just gets the queue and calls remove() for
> each canceled Future.  That would be worse, it would take the hit all at
> once.

Now I see.

TPE.purge() iterates through the queue and calls iterator.remove() at
the appropriate times.

However, DelayQueue's Iterator implementation re-walks the queue
(again) for each item that is removed.

So I no longer think that ScheduledThreadPoolExecutor.purge() is more
efficient than removing each item separately -- and I'm unhappy.

--Joe


More information about the Concurrency-interest mailing list