C# 关于Parallel并行

并行的两种方式


        private void Form1_Load(object sender, EventArgs e)
        {
           CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            CancellationToken cancellationToken = cancellationTokenSource.Token;
            Task.Factory.StartNew(() =>
            {
                Thread.Sleep(1);
                cancellationTokenSource.Cancel();
            });

            parallerdegreecancel(cancellationTokenSource);
        }


        private void parallelfor()
        {
            int totalcount = 0;
            var files = Directory.GetFiles("C:\\");
            Parallel.For(0, files.Length, (i) =>
            {
                FileInfo fileInfo = new FileInfo(files[i]);
                if (fileInfo.CreationTime.Day == DateTime.Now.Day)
                {
                    Interlocked.Increment(ref totalcount);
                }
            });
        }

        private void parallerforeach()
        {
            List<string> vs = new List<string>() { "www.google.com","www.baidu.com"};
            List<string> vs2 = new List<string>();
            Parallel.ForEach(vs, (i) => 
            { 
                Ping  ping = new Ping();
                //这是线程不安全的
                vs2.Add($"{ping.Send(i).Status}");
             }
            );
        }

设置最大并行度

// 并行度,通过添加ParallelOptions 设置MaxDegreeOfParallelism,设置最大的并行数量
        private void parallerdegree()
        {
            List<string> vs = new List<string>() { "www.google.com", "www.baidu.com" };
            List<string> vs2 = new List<string>();
            Parallel.ForEach(vs,new ParallelOptions() { MaxDegreeOfParallelism=1}, (i) =>
            {
                Ping ping = new Ping();
                vs2.Add($"{ping.Send(i).Status}");
            }
            );
        }

通过cancellationtoken取消并行

//关于并行的停止cancel,通过设置ParallelOptions,添加CancellationToken
        private void parallerdegreecancel(CancellationTokenSource cancellationTokenSource)
        {
            List<string> vs = new List<string>() { "www.google.com", "www.baidu.com" };
            List<string> vs2 = new List<string>();
            Parallel.ForEach(vs, new ParallelOptions() { MaxDegreeOfParallelism = 1 ,CancellationToken= cancellationTokenSource .Token}, (i, parallelloopstate) =>
            {
                try
                {
                    //parallelloopstate.Stop();
                    Ping ping = new Ping();
                    vs2.Add($"{ping.Send(i).Status}");
                }
                catch (Exception)
                {

                    throw;
                }
               
            }
            );
        }

通过parallelloopstate取消并行

        //关于并行的停止break/stop
        private void parallerdegreebreak()
        {
            List<string> vs = new List<string>() { "www.google.com", "www.baidu.com" };
            List<string> vs2 = new List<string>();
            Parallel.ForEach(vs, new ParallelOptions() { MaxDegreeOfParallelism = 1 }, (i,parallelloopstate) =>
            {
                parallelloopstate.Break();
                //parallelloopstate.Stop();
                Ping ping = new Ping();
                vs2.Add($"{ping.Send(i).Status}");
            }
            );
        }

你可能感兴趣的:(C#,c#,服务器,开发语言)