zoukankan      html  css  js  c++  java
  • C# Thread多线程学习

    自我学习理解:一个程序中包括多个进程,每个进程包括多个线程,多个线程可同时做不同的事情(说是同时,但它是交换执行的,人感觉像是同时罢了)。

    优点:提高CPU的使用率。

    线程同步:同步就是指一个线程要等待上一个线程执行完之后才开始执行当前的

    线程异步:线程异步是指一个线程去执行,它的下一个线程不必等待它执行完就开始执行

    缺点:耗内存.

            线程多时难以管理,易出现Bug。

    命名空间:using System.Threading; 

    多线程的几种状态:

    Thread类有几个至关重要的方法,描述如下:
    Start():启动线程;
    Sleep(int):静态方法,暂停当前线程指定的毫秒数;
    Abort():通常使用该方法来终止一个线程;
    Suspend():该方法并不终止未完成的线程,它仅仅挂起线程,以后还可恢复;
    Resume():恢复被Suspend()方法挂起的线程的执行;

    thread.Join():等线程执行完后结束线程。

    Thread的5种优先级:Highest(最高),AboveNormal(高于正常),Normal(正常),BelowNormal(低于正常),Lowest(最低)

    优先级的意思并不是说哪个线程先执行,而是CPU给由优先级高的分配的时间片会多一些。高的比低的执行的次数会多一些,但是顺序不确定。

    多线程的简单使用

    static void Main(string[] args)
            {
                Program a = new Program();
                a.CreateThread();
            }
    
            Thread thread1 = null;
            Thread thread2 = null;
            event EventHandler CloseEvent;//定义线程执行完后的事件
            List<int> lis = new List<int>();
            private void CreateThread()
            {
                thread1 = new Thread(new ThreadStart(function1));
                thread1.Priority = ThreadPriority.Highest;//设置最高优先级
                thread2 = new Thread(new ThreadStart(function1));
                thread2.Priority = ThreadPriority.Lowest;//设置最低优先级
    
                thread1.Name = "php1";//线程名称
                thread2.Name = "php2";
                CloseEvent += new EventHandler(threadClose);//线程结束时调用
                thread1.Start();//启动线程开始执行
                thread2.Start();
            }
    
            void function1()
            {
                while (true)
                {
                    //Monitor.Enter(this);//锁定,保持同步
                    if (lis.Count == 100)
                    {
                        CloseEvent(this, new EventArgs());
                    }
                    else
                    {
                        int a = new Random().Next(10);
                        lis.Add(a);
    
                        Console.WriteLine(Thread.CurrentThread.Name+"线程添加了" + a);
    
                    }
                    //Monitor.Exit(this);//取消锁定
                    //Thread.Sleep(1000);//暂停多少毫秒后执行
                    //thread1.Suspend();//挂起线程
                    //thread1.Resume();//恢复挂起的线程
                }
            }
            void function2()
            {
                while (true)
                {
                    //Monitor.Enter(this);//锁定,保存同步
                    if (lis.Count == 100)
                    {
                        CloseEvent(this, new EventArgs());
                    }
                    else
                    {
                        int b = new Random().Next();
                        lis.Add(b);
                        Console.WriteLine("线程2添加了" + b);
                    }
                    //Monitor.Exit(this);//取消锁定
                    //Thread.Sleep(1000);//暂停多少毫秒后执行
                    
                }
            }
    
            void threadClose(object sender, EventArgs e)
            {
    
                Console.WriteLine("线程运行结束!");
                Console.ReadLine();
                thread1.Abort();
                thread2.Abort();
            }
    

      

      

      

     



  • 相关阅读:
    WinInet中的FTP操作
    CodeIgniter 用户指南 版本 1.7.2
    《Windows Mobile实例开发》电子书提供下载
    程序静默安装的参数总结
    Select a table of certain webpage
    568A
    在IIS 5.1 或IIS6 中配置PHP 的FastCGI模式
    镁天三国育将篇
    镁天三国军事篇
    windows 环境下的 protoc 安装
  • 原文地址:https://www.cnblogs.com/Evan-Pei/p/3332672.html
Copyright © 2011-2022 走看看