C# 多线程

C# 多线程

进程与线程

进程:进程就是一个应用程序,对电脑的各种资源的占用

线程:线程是程序执行的最小单位,任何操作都是线程完成的,线程依托进程存在的,一个进程可以有多个线程

无参数的子线程

 public static void ChildThread1()
        {
            Console.WriteLine("Child thread1 is starts");
        }

带参数的子线程

ChildThread2是带参数的子线程,所以要使用ParameterizedThreadStart类型的委托来指定子线程
如果使用的是不带参数的委托,不能使用带参数的Start方法运行线程,否则系统会抛出异常。
但使用带参数的委托,可以使用thread.Start()来运行线程,这时所传递的参数值为null。

特别注意:ParameterizedThreadStart委托的参数类型必须是object的

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Test
    {
        public static void ChildThread1()
        {
            Console.WriteLine("Child thread1 is starts");
        }
        //注意:ParameterizedThreadStart委托的参数类型必须是object的
        public static void ChildThread2(object obj)
        {
            Console.WriteLine("Child thread2 is starts,the parameter is {0}", obj);
        }
        public static void Main()
        {
            
            ThreadStart thread1 = new ThreadStart(ChildThread1); //通过ThreadStart委托指定子线程的方法
            ParameterizedThreadStart thread2 = new ParameterizedThreadStart(ChildThread2); //有参的委托
            Console.WriteLine("MainThread:Creating the child thread1");
            Console.WriteLine("MainThread:Creating the child thread2");
            Thread childThread1 = new Thread(thread1); //创建子线程1
            Thread childThread2 = new Thread(thread2);//创建子线程2
            childThread1.Start();    //运行子线程1
            childThread2.Start("子线程2的参数");//运行子线程2,传递参数,
            //如果使用的是不带参数的委托,不能使用带参数的Start方法运行线程,否则系统会抛出异常。
            //但使用带参数的委托,可以使用thread.Start()来运行线程,这时所传递的参数值为null。
            
            Console.ReadKey();
        }
    }
}

运行结果

在这里插入图片描述

销毁线程 Abort()

使用Abort()中止子线程.
通过抛出 threadabortexception 在运行时中止线程。这个异常不能被捕获,如果有 finally 块,控制会被送至 finally 块。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Test
    {
        public static void ChildThread1()
        {
            Console.WriteLine("Child thread1 is starts");
        }
        //注意:ParameterizedThreadStart委托的参数类型必须是object的
        public static void ChildThread2(object obj)
        {
            Console.WriteLine("Child thread2 is starts,the parameter is {0}", obj);
        }
        public static void ChildThread3()
        {
            try
            {
                Console.WriteLine("Child thread3 starts");
                for (int i = 0; i < 5; i++)
                {
                    Thread.Sleep(100);
                    Console.WriteLine(i);
                }
                Console.WriteLine("Child Thread3 Completed");
            }
            catch (ThreadAbortException e)
            {
                Console.WriteLine("Thread Abort Exception");
            }
            finally
            {
                Console.WriteLine("Couldn't catch the thread Exception");
            }
        }
        public static void Main()
        {
            ThreadStart thread3 = new ThreadStart(ChildThread3);
            Console.WriteLine("MainThread:Creating the child thread3");
            Thread thread = new Thread(thread3);
            thread.Start();
            //停止主线程1000
            Thread.Sleep(2000);
            Console.WriteLine("In Main: Aborting the Child thread");
            thread.Abort();
            Console.ReadKey();
        }
    }
}

运行结果

在这里插入图片描述

ThreadPool和Task

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    
    class Test
    {
        public static Thread t = null;
        public static void ChildThread1()
        {
            int i = 5;
            while (i > 0)
            {
                Console.WriteLine(string.Format("线程【1】的i:{0} ", i));
                Thread.Sleep(10);
                i--;
            }
            Console.WriteLine("线程【1】结束");

            //Console.WriteLine("Child thread1 is starts");
        }
        //注意:ParameterizedThreadStart委托的参数类型必须是object的
        public static void ChildThread2(object obj)
        {
            int i = 5;
            while (i > 0)
            {
                Console.WriteLine(string.Format("线程【2】的i:{0} ", i));
                Thread.Sleep(10);
                i--;
            }
            Console.WriteLine("线程【2】结束");
        }
        public static void ChildThread3()
        {
            int i = 5;
            while (i > 0)
            {
                Console.WriteLine(string.Format("线程【3】的i:{0} ", i));
                Thread.Sleep(10);
                i--;
            }
            Console.WriteLine("线程【3】结束");
        }
        
        public static void Main(string[] args)
        {
            t = new Thread(new ThreadStart(ChildThread1));
            t.Start();

            //用线程池
            ThreadPool.QueueUserWorkItem(ChildThread2, new object());

            //用Task方法创建
            System.Threading.Tasks.Task.Factory.StartNew(ChildThread3);
            Console.ReadLine();
        }
    }
}

运行结果

在这里插入图片描述
线程都是独立的,

相关推荐

  1. C++ 线

    2024-03-30 09:42:04       27 阅读
  2. C++ 线

    2024-03-30 09:42:04       18 阅读
  3. C#线

    2024-03-30 09:42:04       12 阅读
  4. C++ 线

    2024-03-30 09:42:04       11 阅读
  5. 线C#】

    2024-03-30 09:42:04       8 阅读
  6. C++ 线 atomic

    2024-03-30 09:42:04       46 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-30 09:42:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-30 09:42:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-30 09:42:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-30 09:42:04       20 阅读

热门阅读

  1. Python数据库编程全指南SQLite和MySQL实践

    2024-03-30 09:42:04       18 阅读
  2. 【统计】什么事 R 方

    2024-03-30 09:42:04       20 阅读
  3. 机器学习概念、步骤、分类和实践

    2024-03-30 09:42:04       18 阅读
  4. openGauss JDBC客户端负载均衡与读写分离

    2024-03-30 09:42:04       20 阅读
  5. 文件系统知识内容详解

    2024-03-30 09:42:04       19 阅读