queue with time stamped elements within a time period

后端 未结 3 2245
孤街浪徒
孤街浪徒 2021-02-20 14:29

I want to store in a queue, datastructure does not matter, only the elements that I have inserted within say last 5 minutes from current time. Anything older should get removed

3条回答
  •  面向向阳花
    2021-02-20 15:20

    In what language? Is the queue persistent or in-memory?

    If you need this behavior in Java, you can use a DelayedQueue, and have a separate thread calling queue.take() continuously in a tight loop to drain out expired items. queue.size() will then give you the size of remaining unexpired items in the queue. This requires that the items you put in the DelayedQueue implement the Delayed interface and return the value 5 minutes to the .getDelay() method.

提交回复
热议问题