简体   繁体   English

我如何存储实时数据

[英]How do i store real-time data

I am developing a stock market application in windows form using .net framework 4.0. 我正在使用.net framework 4.0开发Windows窗体的股票市场应用程序。 I am fetching ticks from the exchange real-time. 我正在从交易所实时获取蜱虫。 I am able to receive the ticks on real-time basis. 我能够实时收到报价。 In my scenario I have to create a thread which will write the real-time feeds in generic collection & other continuously running background thread will fetch it from the collection & does further processing. 在我的场景中,我必须创建一个线程,它将在通用集合中编写实时源,其他连续运行的后台线程将从集合中获取它并进行进一步处理。 To achieve this i am using the combination of ConcurrentDictionary and ConcurrentQueue . 为了实现这一点,我使用了ConcurrentDictionaryConcurrentQueue的组合。 Key conatins symbol name & value contains ConcurrentQueue. Key conatins符号名称和值包含ConcurrentQueue。 This is how it has been implemented Check first answer 这是如何实现检查第一个答案

This is working just fine my only concern is the delay. 这工作得很好我唯一关心的是延迟。 This process is causing 1 second delay as stocks prices have to be fluctuated on real-time basis. 由于股票价格必须实时波动,这个过程导致1秒的延迟。 Can this scenario be implemented by some other logic or can i improve the performance of ConcurrentDictionary or ConcurrentQueue somehow in order to avoid delays? 这个场景可以通过其他一些逻辑实现,还是可以以某种方式提高ConcurrentDictionary或ConcurrentQueue的性能以避免延迟?

Do you really need a concurrent dictionary? 你真的需要并发字典吗? Your dictionary changes only when you find a new symbol. 只有在找到新符号时,您的字典才会更改。 If you happen to know all the symbols in advance, you can create an ordinary dictionary, fill it with a queue per symbol and that's it - the dictionary never changes beyond that. 如果您碰巧事先知道所有符号,您可以创建一个普通的字典,每个符号填充一个队列,就是这样 - 字典永远不会改变。

I'm not sure how your processing thread works, though - how does it know which queue to look at next? 我不确定你的处理线程是如何工作的 - 它如何知道接下来要查看哪个队列? Does it loop over all the queues to see if there is some more data? 它是否遍历所有队列以查看是否还有更多数据? Perhaps what you need is just one ConcurrentQueue that will hold all the stock market events, and divide it into separate containers in the background there (perhaps with no synchronization). 也许您需要的只是一个ConcurrentQueue ,它将保存所有股票市场事件,并将其分配到后台的单独容器中(可能没有同步)。

Why don't you Enqueue an object which contains stmbol-id and corresponding Page, Dequeue it by the worker threads (without assigning a dedicated thread to each symbol id), and then, the worker thread will send the page to the correct processing depending on the symbol id by using a simple switch case statement. 为什么不将包含stmbol-id和相应Page的对象排入队列,由工作线程对其进行队列化(不为每个符号id分配专用线程),然后,工作线程将页面发送到正确的处理,具体取决于通过使用简单的switch case语句在符号id上。

this way, you don't have to check whether a Queue exists for a symbol. 这样,您不必检查符号是否存在队列。 You'll only have a single thread-safe Queue of type AND you could use the ThreadPool for the processing! 您将只有一个类型为AND的线程安全队列,您可以使用ThreadPool进行处理!

the worker threads will do this: 工作线程将执行此操作:

IdPagePair pair = market.Dequeue();

if (pair.Id == 1) { Process1(pair.Page); }

else 

if (pair.Id == 2) { Process2(pair.Page); }

// etc...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM