[untitled] c\5 multi thread method for processing multiple data http://www.bdgxy.com/

Article source: Learning passhttp://www.bdgxy.com/

General learning networkhttp://www.boxinghulanban.cn/

Zhixue.comhttp://www.jaxp.net/

excel tutorial of table makinghttp://www.tpyjn.cn/

Learning passhttp://www.tsgmyy.cn/


catalogue

summary

Multithreading refers to the technology of implementing concurrent execution of multiple threads from software or hardware. Computers with multithreading capability can execute more than one Thread at the same time due to hardware support, thus improving the overall processing performance. Systems with this capability include symmetric multiprocessors, multi-core processors, and chip level multiprocessing or simultaneous multithreading processors. In a program, these independently running program segments are called "threads", and the concept of programming with them is called "multithreading".

A Queue represents a collection of first in first out objects. Queues are used when you need first in first out access to items. When you add an item to the list, it is called enqueued. When you remove an item from the list, it is called dequeued.

For example, when we are dealing with scheduled tasks, if there is only one machine, it is impossible for us to run one data per thread. At this time, we need to improve the utilization of machine resources.

Let's introduce how to implement multithreading + queue to improve the concurrent processing capability.

code implementation

1. Define the number of threads threadNum and queue queues

  /// <summary>
    ///Total threads
    /// </summary>
    private int threadNum = 4;
/// &lt;summary&gt;
///Total
/// &lt;/summary&gt;
private int totalCount = 0;

/// &lt;summary&gt;
///Processed
/// &lt;/summary&gt;
private int index = 0;

/// &lt;summary&gt;
///Queue
/// &lt;/summary&gt;
private ConcurrentQueue&lt;AssetRepayment&gt; queues = new ConcurrentQueue&lt;AssetRepayment&gt;();</pre>

2. Define the thread list and add data to the thread

  public void SubDeTransaction()
    {
      var list = new List<AssetRepayment>();
      for (int i = 0; i < 1000; i++)
      {
        list.Add(new AssetRepayment() { Title = i.ToString() + "---" + Guid.NewGuid().ToString() });
      }
  if (list == null || list.Count() == 0)
  {
    Console.WriteLine("No executable data");
    return;
  }
  totalCount = list.Count;
  Console.WriteLine("Executable data:" + list.Count() + "strip");
  foreach (var item in list)
  {
    queues.Enqueue(item);
  }
  List&lt;Task&gt; tasks = new List&lt;Task&gt;();
  for (int i = 0; i &lt; threadNum; i++)
  {
    var task = Task.Run(() =&gt;
    {
      Process();
    });
    tasks.Add(task);
  }
  var taskList = Task.Factory.ContinueWhenAll(tasks.ToArray(), (ts) =&gt;
  {
  });
  taskList.Wait();
}</pre>

3. Limit the number of threads for (int i = 0; I < threadnum; i++)

  var taskList = Task.Factory.ContinueWhenAll(tasks.ToArray(), (ts) =>
      {
      });
      taskList.Wait();

4. Fetch data from the queue for business processing

  private void Process()
    {
      while (true)
      {
        var currentIndex = Interlocked.Increment(ref index);
        AssetRepayment repayId = null;
        var isExit = queues.TryDequeue(out repayId);
        if (!isExit)
        {
          break;
        }
        try
        {
          Console.WriteLine(repayId.Title);
      Console.WriteLine(string.Format(" common{0}Article current article{1}strip", totalCount, currentIndex));
    }
    catch (Exception ex)
    {
      Console.WriteLine(ex);
    }
  }
}</pre>

Code address

https://gitee.com/conanOpenSource_admin/Example

The above is the details of the method of c\multi-threaded processing multiple data. For more information about c\multi-threaded processing multiple data, please follow the novice tutorial https://www.piaodoo.com/ Other related articles!

Tags: Java C#

Posted by Huuggee on Mon, 30 May 2022 15:15:25 +0530