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

 


相关文章
|
22小时前
|
安全 Java 调度
Java一分钟:多线程编程初步:Thread类与Runnable接口
【5月更文挑战第11天】本文介绍了Java中创建线程的两种方式:继承Thread类和实现Runnable接口,并讨论了多线程编程中的常见问题,如资源浪费、线程安全、死锁和优先级问题,提出了解决策略。示例展示了线程通信的生产者-消费者模型,强调理解和掌握线程操作对编写高效并发程序的重要性。
10 3
|
3天前
|
Java
【Java多线程】面试常考 —— JUC(java.util.concurrent) 的常见类
【Java多线程】面试常考 —— JUC(java.util.concurrent) 的常见类
12 0
|
3天前
|
Java API 调度
【Java多线程】Thread类的基本用法
【Java多线程】Thread类的基本用法
6 0
|
3天前
|
安全 Java C#
C#多线程详解
C#多线程详解
12 0
|
3天前
|
C#
c# 所有类的最终基类:Object
c# 所有类的最终基类:Object
5 0
|
3天前
|
安全 C#
C#Webrequest类的使用
C#Webrequest类的使用
7 0
|
4天前
|
算法 安全 调度
【C++入门到精通】 线程库 | thread类 C++11 [ C++入门 ]
【C++入门到精通】 线程库 | thread类 C++11 [ C++入门 ]
14 1
|
6天前
|
SQL 存储 C#
C# Web控件与数据感应之 TreeView 类
C# Web控件与数据感应之 TreeView 类
|
21天前
|
开发框架 前端开发 .NET
C#编程与Web开发
【4月更文挑战第21天】本文探讨了C#在Web开发中的应用,包括使用ASP.NET框架、MVC模式、Web API和Entity Framework。C#作为.NET框架的主要语言,结合这些工具,能创建动态、高效的Web应用。实际案例涉及企业级应用、电子商务和社交媒体平台。尽管面临竞争和挑战,但C#在Web开发领域的前景将持续拓展。
|
21天前
|
SQL 开发框架 安全
C#编程与多线程处理
【4月更文挑战第21天】探索C#多线程处理,提升程序性能与响应性。了解C#中的Thread、Task类及Async/Await关键字,掌握线程同步与安全,实践并发计算、网络服务及UI优化。跟随未来发展趋势,利用C#打造高效应用。