zoukankan      html  css  js  c++  java
  • Task/Parallel实现异步多线程

    代码:

    #region Task 异步多线程,Task是基于ThreadPool实现的
                {
                    //TestClass testClass = new TestClass();
                    //Action<object> action = new Action<object>(t => testClass.TestThread(t.ToString()));
    
                    //TaskFactory taskFactory = new TaskFactory();
                    //List<Task> taskList = new List<Task>();
                    //for (int i = 0; i < 5; i++)
                    //{
                    //    Task task = taskFactory.StartNew(action, "task" + i);
                    //    taskList.Add(task);
                    //}
    
                    //同步等待
                    ////1.1所在线程等待,目前在主线程,等待某一个Task执行完毕,只要有一个完成,就继续往下执行。会卡住主线程。
                    //Task.WaitAny(taskList.ToArray());
                    //Console.WriteLine("某一个Task执行完毕");
    
                    ////1.2所在线程等待,目前在主线程,直到所有Task执行完毕;会卡住主线程。
                    //Task.WaitAll(taskList.ToArray());
                    //Console.WriteLine("所有Task执行完毕");
    
                    ////2.1回调等待
                    ////不卡主线程,所有Task完成,才执行下面的操作
                    //taskFactory.ContinueWhenAll(taskList.ToArray(), taskArray =>
                    //{
                    //    Console.WriteLine("taskFactory.ContinueWhenAll {0}", Thread.CurrentThread.ManagedThreadId);
                    //    foreach (var item in taskArray)
                    //    {
                    //        Console.WriteLine(item.AsyncState);
                    //        Console.WriteLine(item.IsCompleted);
                    //    }
                    //});
    
                    ////2.2回调等待
                    ////不卡主线程,有一个Task完成,就执行下面的操作
                    //taskFactory.ContinueWhenAny(taskList.ToArray(), taskAction =>
                    //{
                    //    Console.WriteLine("taskFactory.ContinueWhenAny {0}", Thread.CurrentThread.ManagedThreadId);
                    //    Console.WriteLine(taskAction.AsyncState);
                    //    Console.WriteLine(taskAction.IsCompleted);
                    //});
                }
                #endregion
    
                #region Parallel 基于Task实现,多个任务并行计算,主线程也会计算,其实就是Task+WaitAll,一定会卡住主线程
                {
                    //Console.WriteLine("主线程的ID:{0}", Thread.CurrentThread.ManagedThreadId);
                    //Parallel.Invoke(() => { Console.WriteLine("当前线程的ID:{0}", Thread.CurrentThread.ManagedThreadId); },
                    //    () => { Console.WriteLine("当前线程的ID:{0}", Thread.CurrentThread.ManagedThreadId); },
                    //    () => { Console.WriteLine("当前线程的ID:{0}", Thread.CurrentThread.ManagedThreadId); },
                    //    () => { Console.WriteLine("当前线程的ID:{0}", Thread.CurrentThread.ManagedThreadId); },
                    //    () => { Console.WriteLine("当前线程的ID:{0}", Thread.CurrentThread.ManagedThreadId); });
                    ////全部完成后,进入下一步,看上去就像同步编程
    
                    //Parallel.ForEach<int>(new int[] { 1, 2, 3, 4, 5 }, t =>
                    //{
                    //    Console.WriteLine("当前线程ID:{0},结果:{1}", Thread.CurrentThread.ManagedThreadId, t * t);
                    //    Thread.Sleep(100);
                    //});
    
                    //
                    ParallelOptions options = new ParallelOptions()
                    {
                        MaxDegreeOfParallelism = 5
                    };
                    Parallel.For(0, 1000, options, t => { Console.WriteLine("结果:" + t.ToString()); });
                    Parallel.For(0, 1000, options, (t, state) => {
                        Console.WriteLine("结果:" + t.ToString());
                        //state.Break();//
                        //state.Stop();//
                        //return;
                    });
                }
                #endregion
  • 相关阅读:
    Nginx 提示500 ,设置提示具体错误
    部署laravel 到linux环境
    php & vue 跨域问题解决方案
    一个php学习vue技术(篇章1)
    中年危机
    elasticsearch 创建索引、unassigned、elasticsearch 创建索引后出现unassigned、
    laravel jwt 报错 Argument 3 passed to LcobucciJWTSignerHmac::doVerify() must be an instance of LcobucciJWTSignerKey, null given
    laravel jwttoken jwt attempt laravel auth->attempt() 返回false
    composer 报错 Carbon 1 is deprecated, see how to migrate to Carbon 2
    php 服务端主动向客户端推送消息, php swoole websocket, 服务端向客户端推送消息, websocket主动推送消息
  • 原文地址:https://www.cnblogs.com/xsj1989/p/7833682.html
Copyright © 2011-2022 走看看