其他分享
首页 > 其他分享> > 学习笔记(11)--NetFramework Thread版本

学习笔记(11)--NetFramework Thread版本

作者:互联网

NetFramework 1.0 1.1,Thread是最初版本,功能非常强大,唯一的前台线程。

ThreadStart threadStart = new ThreadStart(() =>

                {

                    Console.WriteLine($"************   Thread  start。。。{Thread.CurrentThread.ManagedThreadId}");

                    Thread.Sleep(2000);

                    Console.WriteLine($"************   Thread  end。。。{Thread.CurrentThread.ManagedThreadId}");

                });

                Thread thread = new Thread(threadStart);

                thread.Start();

直到NetFramework 4.7版本,在1.01.1版本都这么强大了,为什么还会有新的版本出来,正是它强大过头了。1.01.1版本的线程是没有管控的,可以new无限个Thread出来,容易出问题。

到NetFramework 2.0版本,ThreadPool线程池,池化线程管理,进程唯一一个实例,其中会默认申请10个线程,线程需要线程时,就先池子申请,获取并使用,用完后回归线程池;如果线程不够用,会由池子CLR申请,如果闲着太多,就池子负责释放,能起到线程重用,管控线程最大数量。

WaitCallback callback = new WaitCallback(ar =>

                {

                    Console.WriteLine($"************   callback  start。。。{Thread.CurrentThread.ManagedThreadId}");

                    Thread.Sleep(2000);

                    Console.WriteLine($"************   callback  end。。。{Thread.CurrentThread.ManagedThreadId}");

                });

                ThreadPool.QueueUserWorkItem(callback);

到NetFramework 3.0版本Task 基于线程池的线程  API丰富。

Action action = new Action(() =>

               {

                   Console.WriteLine($"************   Task  start。。。{Thread.CurrentThread.ManagedThreadId}");

                   Thread.Sleep(2000);

                   Console.WriteLine($"************   Task  end。。。{Thread.CurrentThread.ManagedThreadId}");



               });

                Task.Run(action);

.NetFramework 4.0   Parallel对Tack封装,主线程参与计算 option可以限制最大线程数

Parallel.Invoke(() =>

                {

                    Console.WriteLine($"************   Parallel1  start。。。{Thread.CurrentThread.ManagedThreadId}");

                    Thread.Sleep(2000);

                    Console.WriteLine($"************   Parallel1  end。。。{Thread.CurrentThread.ManagedThreadId}");



                },

                () =>

                {

                    Console.WriteLine($"************   Parallel2  start。。。{Thread.CurrentThread.ManagedThreadId}");

                    Thread.Sleep(2000);

                    Console.WriteLine($"************   Parallel2  end。。。{Thread.CurrentThread.ManagedThreadId}");

                },

                () =>

                {

                    Console.WriteLine($"************   Parallel3  start。。。{Thread.CurrentThread.ManagedThreadId}");

                    Thread.Sleep(2000);

                    Console.WriteLine($"************   Parallel3  end。。。{Thread.CurrentThread.ManagedThreadId}");                },

                () =>

                {

                    Console.WriteLine($"************   Parallel4  start。。。{Thread.CurrentThread.ManagedThreadId}");

                    Thread.Sleep(2000);

                    Console.WriteLine($"************   Parallel4  end。。。{Thread.CurrentThread.ManagedThreadId}");

                });

标签:11,NetFramework,Console,CurrentThread,Thread,ManagedThreadId,线程,WriteLine
来源: https://blog.csdn.net/qq_44484973/article/details/91359970