简体   繁体   中英

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

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)

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.

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. Tasks is the way to go and Thread pool is managed automatically for you.

What may suite your application would depend on what happens in process_txn and is subjective, so this is very generic guideline:

  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 . It will help you use the CPU cores more efficiently.

  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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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