java - which data structure to use to store the entries with their time stamp in sorted order -
i have implement 1 scenario in need store large number of entries timestamps "anystring" "timestamp". now, have check periodically entries timer going expire (let's entries expired timestamp more hour old). so, in order keeping them alive, have filter out entries timer going expire within time , perform operations , reinitialize timestamp. multithreaded environment.
in order looking efficient data structure when scan map find out timer going expire, not have traverse entire map. traverse complete map each time filter out expire entries have huge performance impact.
i think of using "concurrentskiplistmap" in comparator can used store entries in sorted order of timestamps. each time scan map till netires having timestamp greater required value.
is there better way accomplish task? thanks.
short: suggest http://netty.io/4.0/api/io/netty/util/hashedwheeltimer.html - want.
long:
there special kind of structures purpose: timer wheels. here
in short, idea following: have large circular array, each element of array list of objects. have pointer, points element of array, , incremented on each tick. each element has associated time ranges, chosen in special way. e.g. whole wheel round 1s, array has 1000 elements, then: 0th element list of events, should fire @ x.000 - x.001s, 1st element list of events, should fire @ x.001 - x.002s etc.
when add new event, should reminder time_when_the_event_should_fire / wheel_period, , way determine element of array should add event.
the pointer incremented 1 on each tick (1 ms), elements ordered lists - on each tick take element (list), iterate through list items, , if event should fire @ tick, fire it, otherwise stop iterating.
thus you'll o(1) operations, , o(n/wheel_size) on adding new events.
Comments
Post a Comment