• C#多线程3种创建Thread、Delegate.BeginInvoke、线程池


    1   创建多线程,一般情况有以下几种:(1)通过Thread类   (2)通过Delegate.BeginInvoke方法   (3)线程池

    using System;  

    using System.Collections.Generic;  

    using System.Linq;  

    using System.Text;  

    using System.Timers;  

    using System.Threading;  

    using System.IO;  

    using System.Xml;  

     namespace XMLTest  

     {  

        public class XmlTest  

         {  

             delegatevoid Delegate_Test(int a);  

      

            staticvoid Main(string[] args)  

            {  

                //通过Thread类,其实也是通过ThreadStart委托  

                 Thread t1 = new Thread(new ThreadStart(fun1));  

                 t1.Start();  

                // 通过委托,然后使用BeginInvoke方法  

                 Delegate_Test dt = new Delegate_Test(fun2);  

                 dt.BeginInvoke(10,null,null);  

      

                 //线程池,使用WaitCallback  

                ThreadPool.QueueUserWorkItem(new WaitCallback(fun3),3);  

                Console.Read();  

            }  

             publicstaticvoid fun1()  

             {  

                 while(1>0)  

                 {  

                    Console.WriteLine("来自普通Thread");  

                    Thread.Sleep(800);  

                 }  

            }  

            publicstaticvoid fun2(int a)  

            {  

                 while(a>0)  

                 {  

                     Console.WriteLine("来自beginInvoke");  

                     Thread.Sleep(1500);  

                }  

                 

             }  

      

            staticvoid fun3(Object o)  

            {  

                while (1> 0)  

                {  

                    Console.WriteLine("来自ThreadPool");  

                    Thread.Sleep(2050);  

                }  

             }  

        }  

    }  

     

  • 相关阅读:
    乌龟git
    CI的model层的操作
    排序算法
    linux安装教程
    linux命令Netstat
    linux压缩和解压缩
    标准库模块time,datetime
    为什么计算机时间和众多编程语言要从1970年1月1日开始算起
    模块和包的导入
    Python封装应用程序的最佳项目结构是什么?
  • 原文地址:https://www.cnblogs.com/hucaihao/p/3514790.html
Copyright © 2020-2023  润新知