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!

提示:本人准备建立一个技术交流群,会将日常学习工作中遇到的问题和解决方案进行分享,同时也会将代码和学习资料上传进去,有什么不懂的问题可以咨询我!+v:SJS66-12

生活所迫打个广告,本人也代购莆田鞋,不是中间商,工厂直接取货,价格优惠质量保证,都是我自己前去挑选,可以视频选购验货!!希望大家支持!!!赚点生活费!!!+v:SJS66-12

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常用让子线程完全终止!
 

参考文档:

C#Thread_c# thread_^命铭的博客-CSDN博客C#多线程Thread类的使用(一)_c# thread用法-CSDN博客C# Thread(线程)使用总结_c#线程一直运行-CSDN博客C# 多线程一: Thread 的简单理解与运用_c# thread-CSDN博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值