I have a uniprocessor dual-threaded application. In stream 1, you will hear a market data feed and update the latest quote on thousands of stocks. In stream 2, a timer with a sampling frequency will start and take a snapshot of the latest quotes for processing. In fact, I need to downsample an extremely fast market data feed.
My first guess is to use a BlockingQueue. To do this, I need to move the timer functions to Thread 1, which I can do by checking the clock every time a quote update arrives and sends a quote snapshot to the queue at the sample rate. My concern is that the queue will consume a lot of memory, and garbage collection will slow down.
My second suggestion is for Thread 1 to copy the data to a locked element at the sampling rate that Thread 2 can access. My concern is that locks will be slow.
My mom guesses this to make quote primitives unstable. Since one thread only writes and only one thread reads, maybe this is suitable?
Is there a better way to transfer data between threads for this delay-sensitive application? This is not an ultra-high frequency application. I can tolerate latencies of the order of tens of ms.
source share