zoukankan      html  css  js  c++  java
  • 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);  

                }  

             }  

        }  

    }  

     

  • 相关阅读:
    Oracle连接数一直在增
    ora00020: maximum number of processes (150) exeeded
    oracle归档日志满了
    C# ZPL
    error CS0227: 不安全代码只会在使用 /unsafe 编译的情况下出现
    最全zpl语言指令解析(含乱码)
    ZPL 打印机实例
    ora-01400 无法将NULL插入 ID 解决方法
    windows 选择时间控件(选择日期, 小时分钟秒)
    用户登陆检验----没有优化,大神可以帮忙优化优化
  • 原文地址:https://www.cnblogs.com/hucaihao/p/3514790.html
Copyright © 2011-2022 走看看