使用Thread类可以创建和控制线程

时间:2024-01-07 13:40:14

1.创建线程

  1. static void Main(string[] args)
  2. {
  3. /* Thread类
  4. * 创建控制线程
  5. * 其构造函数接受ThreadStart和ParameterizedThreadStart类型的委托参数
  6. */
  7. Thread t1 = new Thread(ThreadMain);
  8. t1.Name = "66";
  9. //创建了线程后,可以用Start()方法启动线程啦^-^
  10. t1.Start();
  11. Console.WriteLine("主线程");
  12. /* 使用λ表达式创建线程更简洁 */
  13. Thread t2 = new Thread(() => Console.WriteLine("t2线程"));
  14. t2.Start();
  15. //这个就更简洁 ≡(▔﹏▔)≡
  16. new Thread(() => Console.WriteLine("t3线程")).Start();
  17. Console.ReadLine();
  18. /* 得到结果不能保证哪个结果先输出
  19. * 线程由操作系统调度,每次哪个线程在前不同的
  20. */
  21. }
  22. //线程中通过委托ThreadStart调用的方法
  23. static void ThreadMain()
  24. {
  25. Console.WriteLine("t1线程,线程名:" + Thread.CurrentThread.Name + ",该线程托管ID:" + Thread.CurrentThread.ManagedThreadId);
  26. }

static void Main(string[] args)
{
/* Thread类
* 创建控制线程
* 其构造函数接受ThreadStart和ParameterizedThreadStart类型的委托参数
*/
Thread t1 = new Thread(ThreadMain);
t1.Name = "66";
//创建了线程后,可以用Start()方法启动线程啦^-^
t1.Start();

Console.WriteLine("主线程");

/* 使用λ表达式创建线程更简洁 */
Thread t2 = new Thread(() => Console.WriteLine("t2线程"));
t2.Start();

//这个就更简洁 ≡(▔﹏▔)≡
new Thread(() => Console.WriteLine("t3线程")).Start();

Console.ReadLine();

/* 得到结果不能保证哪个结果先输出
* 线程由操作系统调度,每次哪个线程在前不同的
*/
}

//线程中通过委托ThreadStart调用的方法
static void ThreadMain()
{
Console.WriteLine("t1线程,线程名:" + Thread.CurrentThread.Name + ",该线程托管ID:" + Thread.CurrentThread.ManagedThreadId);
}

2.给线程传送数据

方法一:使用带ParameterizedThreadStart委托参数的Thread构造函数,给线程传送数据

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. /* 使用带ParameterizedThreadStart委托参数的Thread构造函数,给线程传送数据 */
  6. //初始化给线程传送的数据
  7. Data d = new Data();
  8. d.Message = "66";
  9. Thread t1 = new Thread(ThreadMainWithParameters);
  10. //使用了ParameterizedThreadStart委托,线程入口必有一object类型参数
  11. t1.Start(d);
  12. Console.ReadLine();
  13. }
  14. //线程中通过委托ParameterizedThreadStart调用的方法
  15. static void ThreadMainWithParameters(object o)
  16. {
  17. Data d = (Data)o;
  18. Console.WriteLine("线程t1,传入参数:" + d.Message);
  19. }
  20. }
  21. //给线程传送数据,需要某个存储数据的类或结构
  22. public struct Data
  23. {
  24. public string Message;
  25. }

class Program
{
static void Main(string[] args)
{
/* 使用带ParameterizedThreadStart委托参数的Thread构造函数,给线程传送数据 */

//初始化给线程传送的数据
Data d = new Data();
d.Message = "66";

Thread t1 = new Thread(ThreadMainWithParameters);
//使用了ParameterizedThreadStart委托,线程入口必有一object类型参数
t1.Start(d);

Console.ReadLine();
}

//线程中通过委托ParameterizedThreadStart调用的方法
static void ThreadMainWithParameters(object o)
{
Data d = (Data)o;
Console.WriteLine("线程t1,传入参数:" + d.Message);
}
}

//给线程传送数据,需要某个存储数据的类或结构
public struct Data
{
public string Message;
}

方法二:创建一个定制类,把线程的方法定义为实例方法,给线程传送数据

  1. //定制类
  2. public class MyThread
  3. {
  4. private string data;
  5. public MyThread(string data)
  6. {
  7. this.data = data;
  8. }
  9. //线程中通过委托ThreadStart调用的定制类中的实例方法
  10. public void ThreadMain()
  11. {
  12. Console.WriteLine("线程t1,传入参数:" + data);
  13. }
  14. }
  15. class Program
  16. {
  17. static void Main(string[] args)
  18. {
  19. /* 创建一个定制类,把线程的方法定义为实例方法,给线程传送数据 */
  20. MyThread obj = new MyThread("66");
  21. Thread t1 = new Thread(obj.ThreadMain);
  22. t1.Start();
  23. Console.ReadLine();
  24. }
  25. }

//定制类
public class MyThread
{
private string data;

public MyThread(string data)
{
this.data = data;
}

//线程中通过委托ThreadStart调用的定制类中的实例方法
public void ThreadMain()
{
Console.WriteLine("线程t1,传入参数:" + data);
}
}

class Program
{
static void Main(string[] args)
{
/* 创建一个定制类,把线程的方法定义为实例方法,给线程传送数据 */

MyThread obj = new MyThread("66");

Thread t1 = new Thread(obj.ThreadMain);
t1.Start();

Console.ReadLine();
}
}