简体   繁体   English

对于我的场景、线程数组、线程池或任务,go 的正确方法是什么?

[英]what would be the right way to go for my scenario, thread array, thread pool or tasks?

I am working on a small microfinance application that processes financial transactions, the frequency of these transaction are quite high, which is why I am planning to make it a multi-threaded application that can process multiple transactions in parallel.我正在开发一个处理金融交易的小型小额信贷应用程序,这些交易的频率非常高,这就是为什么我打算将它变成一个可以并行处理多个交易的多线程应用程序。 I have already designed all the workers that are thread safe,我已经设计了所有线程安全的工人,

what I need help for is how to manage these threads.我需要帮助的是如何管理这些线程。 here are some of my options这是我的一些选择

1.make a specified number of thread pool threads at startup and keep them running like in a infinite loop where they could keep looking for new transactions and if any are found start processing 1.在启动时创建指定数量的线程池线程,并让它们像在无限循环中一样运行,在那里它们可以继续寻找新事务,如果发现任何事务则开始处理

example code:示例代码:

void Start_Job(){
 for (int l_ThreadId = 0; l_ThreadId < PaymentNoOfWorkerThread; l_ThreadId++)
                {
                    ThreadPool.QueueUserWorkItem(Execute, (object)l_TrackingId);
                    
                }
}


void Execute(object l_TrackingId)
{
   while(true)
      {
         var new_txns = Get_New_Txns(); //get new txns if any returns a queue
         while(new_txns.count > 0 ){
                  process_txn(new_txns.Dequeue())
            }
Thread.Sleep(some_time);
      }
}

2.look for new transactions and assign a thread pool thread for each transaction (my understanding that these threads would be reused after their execution is complete for new txns) 2.寻找新交易并为每个交易分配一个线程池线程(我的理解是这些线程在新交易执行完成后将被重用)

example code:示例代码:

void Start_Job(){

while(true){

 var new_txns = Get_New_Txns(); //get new txns if any returns a queue
 for (int l_ThreadId = 0; l_ThreadId < new_txns.count; l_ThreadId++)
                {
                    ThreadPool.QueueUserWorkItem(Execute, (object)new_txn.Dequeue());
                    
                }

}
Thread.Sleep(some_time);

}


void Execute(object Txn)
{
    process_txn(txn);   
}

3.do the above but with tasks. 3.做以上但有任务。

which option would be most efficient and well suited for my application,哪个选项最有效且最适合我的应用程序,

thanks in advance:)提前致谢:)

ThreadPool.QueueUserWorkItem is an older API and you shouldn't be using it directly anymore. ThreadPool.QueueUserWorkItem是一个较旧的 API,您不应再直接使用它。 Tasks is the way to go and Thread pool is managed automatically for you. Tasks是通往 go 的途径,线程池是自动为您管理的。

What may suite your application would depend on what happens in process_txn and is subjective, so this is very generic guideline:什么适合您的应用程序将取决于process_txn中发生的事情并且是主观的,因此这是非常通用的指南:

  1. If process_txn is a compute bound operation: for example it performs only CPU bound calculations, then you may look at the Task Parallel Library .如果process_txn是计算绑定操作:例如它只执行 CPU 绑定计算,那么您可以查看任务并行库 It will help you use the CPU cores more efficiently.它将帮助您更有效地使用 CPU 内核。

  2. If process_txn is less of CPU and more IO bound operations: meaning if it may read/write from files/database or connects to some other remote service, then what you should look at is asynchronous programming and make sure your IO operations are all asynchronous which means your threads are never blocked on IO. This will help your service to be more scalable.如果process_txn占用更少的 CPU 而更多 IO 绑定操作:意味着如果它可以从文件/数据库读取/写入或连接到其他远程服务,那么您应该查看asynchronous programming并确保您的 IO 操作都是异步的意味着您的线程永远不会在 IO 上阻塞。这将有助于您的服务更具可扩展性。 Also depending on what your queue is, see if you can await on the queue asynchronously, so that none of your application threads are blocked just waiting on the queue.还取决于你的队列是什么,看看你是否可以异步等待队列,这样你的应用程序线程就不会因为等待队列而被阻塞。

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

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