Reputation: 31
I have an application which would be recieving stock prices from an external system continously.
The prices will be keyed by the stockID.
As the prices arrive, they will be fed to a blocking queue.
There will be a pool of threads which will consume and process from this queue and put the prices in a cache(hashmap of stock id and price).
What bothers me is if the queue has prices for duplicate stocks id's, when the threads process these, it is possible that the older price is written to the cache after the newer.
Is there anyway to get around this scenario?
How can I make sure that the latest is processed last?
Upvotes: 2
Views: 351
Reputation: 500437
You could tag every incoming price update with an automatically-incremented sequence number. When writing to the cache, you could drop every update whose sequence number is lower than the one already in the cache for that stock.
Alternatively, you could partition your stock universe across threads, meaning that updates for a given stock are always processed by the same thread.
Upvotes: 1