• C#之线程


    1. 线程创建,启动,阻断,监视线程状态

     
    class Program
        {
            public static void Operatefile()
            {
                FileStream fileStream = null;
                if (!File.Exists(@"E:欣奕华项目C#EXThreadExlog.TXT"))
                {
                    fileStream = new FileStream(@"E:欣奕华项目C#EXThreadExlog.txt", FileMode.Create, FileAccess.Write);
                }
                else
                {
                    fileStream = new FileStream(@"E:欣奕华项目C#EXThreadExlog.txt", FileMode.Append, FileAccess.Write);
                }
                StreamWriter streamWriter = new StreamWriter(fileStream);
                Console.WriteLine("线程1:Starting...");
                streamWriter.WriteLine("线程1:Starting...");
                for (int i = 0; i < 10; i++)
                {
                  //  Thread.Sleep(TimeSpan.FromSeconds(2));//暂停2S
                    Console.Write(i.ToString() + "----");
                    Console.WriteLine(Thread.CurrentThread.ThreadState.ToString());
                    streamWriter.Write(i.ToString() + "----");
                    streamWriter.Flush();
                    streamWriter.WriteLine(Thread.CurrentThread.ThreadState.ToString());
                    streamWriter.Flush();
                    //Thread.Sleep(TimeSpan.FromSeconds(2));
                }

                streamWriter.Close();
                streamWriter.Dispose();
                fileStream.Close();
                fileStream.Dispose();
                Console.ReadKey();
            }
            static void PrintNumbers()
            {
                //文件流操作
                FileStream fileStream = null;
                if (!File.Exists(@"E:欣奕华项目C#EXThreadExlog1.TXT"))
                {
                    fileStream = new FileStream(@"E:欣奕华项目C#EXThreadExlog1.txt", FileMode.Create, FileAccess.Write);
                }
                else
                {
                    fileStream = new FileStream(@"E:欣奕华项目C#EXThreadExlog1.txt", FileMode.Append, FileAccess.Write);
                }
                StreamWriter streamWriter = new StreamWriter(fileStream);
                Console.WriteLine("线程2:Starting...");
                streamWriter.WriteLine("线程2:Starting...");
                for (int i = 1; i < 10; i++)
                {
                   // Console.WriteLine(i);
                    Console.Write(i.ToString() + "--");
                    Console.WriteLine(Thread.CurrentThread.ThreadState.ToString());   //线程当前状态
                    streamWriter.Write(i.ToString() + "--");
                    streamWriter.Flush();
                    streamWriter.WriteLine(Thread.CurrentThread.ThreadState.ToString());
                    streamWriter.Flush();
                }
                streamWriter.Close();
                streamWriter.Dispose();
                fileStream.Close();
                fileStream.Dispose();
                Console.ReadKey();
            }
            static void Main(string[] args)
            {
                Thread thread1 = new Thread(Operatefile);
                Thread thread2 = new Thread(PrintNumbers);
                thread1.Start();
                thread1.Join();   //线程阻断,当调用join(),其余线程等待此线程结束后,才可以继续
                bool a = thread1.IsAlive;   //判断线程是否Start
                thread2.Start();
                bool b = thread2.IsAlive;
            }
        }

     工作原理:当程序运行时,启动了一个耗时较长的线程来打印数字,打印每个数字前要等待两秒。但我们在主程序中调用了thread.Join方法,该方法允许我们等待直到线程t完成。当线程t完成 "时,主程序会继续运行。借助该技术可以实现在两个线程间同步执行步骤。第一个线程会等待另一个线程完成后再继续执行。第一个线程等待时是处于阻塞状态(正如暂停线程中调用 Thread.Sleep方法一样)。

    2. 多线程常用函数

    a.启动线程
    顾名思义,“启动线程”就是新建并启动一个线程的意思,如下代码可实现:
    Thread thread1 = new Thread(new ThreadStart( Count));
    其中的 Count 是将要被新线程执行的函数。
    b.杀死线程
    “杀死线程”就是将一线程斩草除根,为了不白费力气,在杀死一个线程前最好先判断它是否还活着(通过 IsAlive 属性),然后就可以调用 Abort 方法来杀死此线程。
    c.暂停线程
    它的意思就是让一个正在运行的线程休眠一段时间。如 thread.Sleep(1000); 就是让线程休眠1秒钟。
    d.优先级
    这个用不着解释了。Thread类中hreadPRiority属性,它用来设置优先级,但不能保证操作系统会接受该优先级。一个线程的优先级可分为5种:Normal, AboveNormal, BelowNormal, Highest, Lowest。具体实现例子如下:
    thread.Priority = ThreadPriority.Highest;
    e.挂起线程
    Thread类的Suspend方法用来挂起线程,直到调用Resume,此线程才可以继续执行。如果线程已经挂起,那就不会起作用。
    if (thread.ThreadState = ThreadState.Running)
    {
    thread.Suspend();
    }
    f.恢复线程
    用来恢复已经挂起的线程,以让它继续执行,如果线程没挂起,也不会起作用。
    if (thread.ThreadState = ThreadState.Suspended)。

  • 相关阅读:
    LeetCode: 389 Find the Difference(easy)
    LeetCode: 669 Trim a Binary Search Tree(easy)
    C++: 内联函数
    C++: STL迭代器及迭代器失效问题
    LeetCode: 371 Sum of Two Integers(easy)
    etcdctl命令
    Etcd介绍
    docker基础镜像打包
    docker常见问题总结
    更改容器内时区
  • 原文地址:https://www.cnblogs.com/xingyuanzier/p/11090281.html
Copyright © 2020-2023  润新知