C#学习系列相关之多线程(二)----Thread类介绍

简介: C#学习系列相关之多线程(二)----Thread类介绍

一、线程初始化

1.无参数

static void Main(string[] args) {
        //第一种写法
        Thread thread = new Thread(test);
        thread.Start();
        //第二种写法 delegate
        Thread thread1 = new Thread(new ThreadStart(test));
        thread1.Start();
        //第三种写法 lambda
        Thread thread2 = new Thread(() => { test(); });
        thread2.Start();
 
        Console.WriteLine("mainThread");
        Console.Read();
    }
    static void test() {
        Console.WriteLine("hello");
    }

2.有参数

static void Main(string[] args) {
        object obj = "xxx";
        //第一种写法
        Thread thread = new Thread(test);
        thread.Start(obj);
        //第二种写法 delegate
        Thread thread1 = new Thread(new ParameterizedThreadStart(test));
        thread1.Start(obj);
        //第三种写法 lambda
        Thread thread2 = new Thread((arg) => { test(arg); });
        thread2.Start(obj);
 
        Console.WriteLine("mainThread");
        Console.Read();
    }
    static void test(object obj) {
        Console.WriteLine("hello" + obj);
    }

注意:1.thread.start()内填入的是实际传递的参数,arg为形参

          2.方法test中传递的参数数量为1,并且必须是object类型

二、线程开启

1.无参数传递

using System;  
using System.Collections.Generic;  
using System.Text;  
using System.Threading;  
 
namespace AAAAAA  
{  
  class AAA  
  {  
  public static void Main()  
  {  
  Thread t = new Thread(new ThreadStart(A));  
  t.Start();  
 
  Console.Read();  
  }  
 
  private static void A()  
  {  
  Console.WriteLine("Method A!");  
  }  
  }  
}

运行结果:Method A!


2.单个参数传递

using System;  
using System.Collections.Generic;  
using System.Text;  
using System.Threading;  
 
namespace AAAAAA  
{  
  class AAA  
  {  
  public static void Main()  
  {   
  Thread t = new Thread(new ParameterizedThreadStart(B));  
  t.Start("B");  
 
  Console.Read();  
  }  
 
  private static void B(object obj)  
  {  
  Console.WriteLine("Method {0}!",obj.ToString ());  
 
  }  
  }  
}

运行结果:Method B!

3.多个参数传递

第一种方法:将多个参数定义为类的属性,类内的方法

using System;  
using System.Collections.Generic;  
using System.Text;  
using System.Threading;  
 
namespace AAAAAA  
{  
  class AAA  
  {  
  public static void Main()  
  {  
  My m = new My();  
  m.x = 2;  
  m.y = 3;  
 
  Thread t = new Thread(new ThreadStart(m.C));  
  t.Start();  
 
  Console.Read();  
  }  
  }  
 
  class My  
  {  
  public int x, y;  
 
  public void C()  
  {  
  Console.WriteLine("x={0},y={1}", this.x, this.y);  
  }  
  }  
}

结果显示:x=2,y=3

第二种方法:该方法最为推荐的方法,定义结构体,通过object进行拆箱,将参数进行传递

//结构体  
  struct RowCol  
  {  
  public int row;  
  public int col;  
  };  
 
//定义方法  
public void Output(Object rc)  
  {  
  RowCol rowCol = (RowCol)rc;  
  for (int i = 0; i < rowCol.row; i++)  
  {  
  for (int j = 0; j < rowCol.col; j++)  
  Console.Write("{0} ", _char);  
  Console.Write("\n");  
  }  
  }

三、常用Thread类下的方法

常用属性:

常用方法介绍:

1、public bool IsBackground { get; set; } //表示此线程是否为后台线程

//false为前台线程,进程结束后,任务执行完毕以后,线程才结束

//true为后台线程,进程结束,线程结束

2、public int ManagedThreadId { get; } //获取当前线程唯一标识符

3、public void Abort(); //终止线程,其实就是抛出个异常

4、public void Suspend(); //挂起也就是暂停线程 (已被弃用)

5、public void Resume(); //将挂起的线程继续,也就是回复线程 (已被弃用)

6、public void ResetAbort(); //是把终止的线程再次启用,都会有延时的

7、public void Sleep(200) ; //线程睡眠

8、public bool Join(int millisecondsTimeout); //会阻塞,必须等到线程结束后才会执行下一步

public bool Join(TimeSpan timeout); //会阻塞,必须等到线程结束后才会执行下一步

9、public static void Sleep(int millisecondsTimeout); //线程睡眠

public static void Sleep(TimeSpan timeout); //线程睡眠

10、public void Start(); //另开线程开始执行

public void Start(object parameter); //另开线程开始,带参数

主要介绍三个方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace 线程test1005
{
    class Program
    {
         static void Main(string[] args)
        {
            Thread tt = new Thread(test);
            tt.Start();
            for (int i = 0; i < 300; i++)
            {
                Console.Write(2);
            }
            Console.Read();
        }
        static void test()
        {
            for (int i = 0; i < 300; i++)
            {
                Console.Write(1);
            }
        }
        
    }
}

正常运行的情况下1,2交替出现;

1.Abort用法

static void Main(string[] args)
        {
            Thread tt = new Thread(test);
            tt.Start();
            tt.Abort();
            for (int i = 0; i < 300; i++)
            {
                Console.Write(2);
            }
            Console.Read();
        }

当我们加入abort后运行结果:支线程被释放

Abort相当于方法内抛出一个异常,会执行方法中catch和finally中的代码

2.thread.ResetAbort用法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace 线程test1005
{
    class Program
    {
         static void Main(string[] args)
        {
            Thread tt = new Thread(test);
            tt.Start();
            Thread.Sleep(10);
            for (int i = 0; i < 300; i++)
            {
                Console.Write(2);
            }
            tt.Abort();
            Console.Read();
        }
        static void test()
        {
            try
            {
                while (true)
                {
                    for (int i = 0; i < 300; i++)
                    {
                        Console.Write(1);
                    }
                }
              
            }
            catch (Exception ex)
            {
                Console.WriteLine("子线程");
                Thread.ResetAbort();
            }
            finally
            {
                Console.WriteLine("这里是finally");
            }
            Console.WriteLine("最后");
        }
        
    }
}

可以看到线程被Abort之后,执行catch和finally块中的内容,但是不会执行finally块之后的内容。

从结果中可以看到,线程被终止了,由于执行了Thread.ResetAbort(),因此就允许继续执行finally块之后的代码。

注意: 如果Thread.ResetAbort()语句放在catch块中,最好应当把Thread.ResetAbort()语句放在catch{}代码块最后,否则会把abortException.ExceptionState中的内容给清空了。Thread.ResetAbort()还可以放在finally块中,它同样也可以允许继续执行finally块之后的代码。另外,Thread.ResetAbort()只能执行一次,不能执行二次及以上,否则会出新的异常。

3.thread.join用法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace 线程test1005
{
    class Program
    {
         static void Main(string[] args)
        {
            Thread tt = new Thread(test);
            tt.Start();
            tt.Join();
            for (int i = 0; i < 300; i++)
            {
                Console.Write(2);
            }
            tt.Abort();
            Console.Read();
        }
        static void test()
        {
            for (int i = 0; i < 300; i++)
            {
                Console.Write(1);
            }
        }
    }
}

运行结果:

tt.join()可以先执行tt线程内的内容,等执行完成后,再执行主线程中的内容

tt.join(1000)线程会等待一段时间(10000ms),若这段时间内工作线程没挂掉,一旦超过这个时间,主线程便会开始工作

小技巧:1.abort()的功能是用来终止调用此方法的线程的,只是在多数情况下,它需要一点时间,有些延迟(可能在短时间内此线程还在执行)...
2.join()方法它的功能不是终止线程,而是在t 线程终止之前,阻止正在结束(调用了abort()方法但还未结束)的t 线程执行,同时使主线程等待,直到t线程终止(也就是abort()方法终止过程完毕)了再执行下面的代码,打印出来的结果,执行状态就为FALSE,线程状态也为停止了。

Join常用让子线程完全终止!

 


相关文章
|
1月前
|
存储 前端开发 算法
C++线程 并发编程:std::thread、std::sync与std::packaged_task深度解析(一)
C++线程 并发编程:std::thread、std::sync与std::packaged_task深度解析
46 0
|
1月前
|
存储 并行计算 Java
C++线程 并发编程:std::thread、std::sync与std::packaged_task深度解析(二)
C++线程 并发编程:std::thread、std::sync与std::packaged_task深度解析
64 0
|
1月前
|
存储 缓存 安全
【C/C++ 关键字 存储类说明符 】 线程局部变量的魔法:C++ 中 thread_local的用法
【C/C++ 关键字 存储类说明符 】 线程局部变量的魔法:C++ 中 thread_local的用法
33 0
|
3天前
|
并行计算 算法 安全
Java从入门到精通:2.1.3深入学习Java核心技术——掌握Java多线程编程
Java从入门到精通:2.1.3深入学习Java核心技术——掌握Java多线程编程
|
17天前
|
存储 安全 Java
java多线程之原子操作类
java多线程之原子操作类
|
19天前
|
Java
Java中的多线程实现:使用Thread类与Runnable接口
【4月更文挑战第8天】本文将详细介绍Java中实现多线程的两种方法:使用Thread类和实现Runnable接口。我们将通过实例代码展示如何创建和管理线程,以及如何处理线程同步问题。最后,我们将比较这两种方法的优缺点,以帮助读者在实际开发中选择合适的多线程实现方式。
23 4
|
21天前
|
Java Spring
springboot单类集中定义线程池
该内容是关于Spring中异步任务的配置和使用步骤。首先,在启动类添加`@EnableAsync`注解开启异步支持。然后,自定义线程池类`EventThreadPool`,设置核心和最大线程数、存活时间等参数。接着,将线程池bean注入到Spring中,如`@Bean(&quot;RewardThreadPool&quot;)`。最后,在需要异步执行的方法上使用`@Async`注解,例如在一个定时任务类中,使用`@Scheduled(cron = &quot;...&quot;)`和`@Async`结合实现异步定时任务。
16 2
|
1月前
|
Linux API C++
【C++ 线程包裹类设计】跨平台C++线程包装类:属性设置与平台差异的全面探讨
【C++ 线程包裹类设计】跨平台C++线程包装类:属性设置与平台差异的全面探讨
51 2
|
1月前
|
Java API C++
【C++ 与Qt 线程】C++ std::thread 与Qt qthread多线程混合编程
【C++ 与Qt 线程】C++ std::thread 与Qt qthread多线程混合编程
45 1
|
1月前
|
安全 C++ 开发者
【C++多线程同步】C++多线程同步和互斥的关键:std::mutex和相关类的全面使用教程与深度解析
【C++多线程同步】C++多线程同步和互斥的关键:std::mutex和相关类的全面使用教程与深度解析
18 0