net tpl 数据流处理 学习 07 MaxDegreeOfParallelism

MaxDegreeOfParallelism 指定并行处理的数量


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;

namespace ExecutionDataflowBlockOptionsDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(" ThreadId:" + Thread.CurrentThread.ManagedThreadId + " Execute Time:" + DateTime.Now);
            TestSync3();
            Console.Read();
        }
        /// <summary>
        /// 指定并行处理的数量,这样会同时使用3个线程进行并发处理
        /// </summary>
        public static ActionBlock<int> abAsync = new ActionBlock<int>((i) =>
        {
            Thread.Sleep(1000);
            Console.WriteLine(i + " ThreadId:" + Thread.CurrentThread.ManagedThreadId + " Execute Time:" + DateTime.Now);
        }
                , new ExecutionDataflowBlockOptions() { MaxDegreeOfParallelism = 3 });

        public static void TestSync3()
        {
            for (int i = 0; i < 10; i++)
            {
                abAsync.Post(i);
            }
            Console.WriteLine("Post finished");
        }
    }
}



作者:spike

分类: Net

创作时间:2023-06-25

更新时间:2024-12-09

联系方式放在中括号之中例如[[email protected]],回复评论在开头加上标号例如:#1