.net 多线程初步(1)

Thread类常用的几个基本方法


1.Start、Join和Sleep
这是线程操作最基本的了,启动、销毁和延迟。


using System;
using System.Threading;

public class StartJoinSleep
{
 public static void Main()
 {
  ThreadStart myThreadStart = new ThreadStart(firstThread);
  Thread myThread = new Thread(myThreadStart);
  myThread.Start();
  Thread.Sleep(1000);
  Console.WriteLine("Now execute join method.");
  myThread.Abort();
  myThread.Join();
  Console.WriteLine("Main Thread ending.Press any key EXIT");
  Console.ReadLine();
 }
 private static void firstThread()
 {
  for ( int i = 1 ; i <= 100 ; i ++)
  {
   Console.WriteLine("First Thread Running. and loop times is {0}.",i);
   Thread.Sleep(200);
  }
 }
}


2.Abort和ResetAbort

Abort在调用此方法的线程上引发 ThreadAbortException,以开始终止此线程的过程。调用此方法通常会终止线程。通常搭配ResetAbort来阻止ThreadAbortException结束线程,用以继续执行finally内的代码
示例:
using System;
using System.Threading;

public class AbortResetAbort
{
 public static void Main()
 {
  ThreadStart myThreadStart = new ThreadStart(Work);
  Thread myThread = new Thread(myThreadStart);
  myThread.Start();
  Thread.Sleep(300);
  Console.WriteLine("Now execute Abort Method.");
  myThread.Abort();
  myThread.Join();
  Console.WriteLine("Main Thread ending.Press any key EXIT");
  Console.ReadLine();   
 }

 public static void Work()
 {
  try
  {
   for(int i=0; i<100; i++)
   {
    Console.WriteLine("Thread - working.");
    Thread.Sleep(100);
   }
  }
  catch(ThreadAbortException e)
  {
   Console.WriteLine("Thread - caught ThreadAbortException - resetting.");
   Console.WriteLine("Exception message: {0}", e.Message);
   Thread.ResetAbort();
  }
  Console.WriteLine("Thread - still alive and working.");
  Thread.Sleep(1000);
  Console.WriteLine("Thread - finished working.");
 }
}

3.Suspend和Resume
线程的挂起与恢复
using System;
using System.Threading;

public class SuspendResume
{
 public static void Main()
 {
  ThreadStart myThreadStart = new ThreadStart(Work);
  Thread myThread = new Thread(myThreadStart);
  myThread.Start();
  Thread.Sleep(500);
  Console.WriteLine("Now execute Suspend Method.2000ms");
  myThread.Suspend();
  Thread.Sleep(2000);
  myThread.Resume();
  myThread.Join();
  Console.WriteLine("Main Thread ending.Press any key EXIT");
  Console.ReadLine();   
 }

 public static void Work()
 {
  for(int i=1; i < 21; i++)
  {
   Console.WriteLine("Thread Loop Times is {0}.",i);
   Thread.Sleep(100);
  }
 }
}

4.Interrupt
将线程在下次以wait、sleep或join状态阻塞的时候中断,下面示例可以查看在Interrupt执行前延迟500ms和550ms时线程内循环次数的区别,可以很清晰的看到触发时间。
using System;
using System.Threading;

public class Interrupt
{
 public static void Main()
 {
  ThreadStart myThreadStart = new ThreadStart(Work);
  Thread myThread = new Thread(myThreadStart);
  myThread.Start();
  Thread.Sleep(500);
  Thread.Sleep(50);
  Console.WriteLine("Now execute Interrupt Method.");
  myThread.Interrupt();
  Thread.Sleep(2000);
  myThread.Join();
  Console.WriteLine("Main Thread ending.Press any key EXIT");
  Console.ReadLine();   
 }
 
 public static void Work()
 {
  int i = 1;
  try
  {
   for(; i < 21; i++)
   {
    Console.WriteLine("Thread Loop Times is {0}.",i);
    Thread.Sleep(100);
   }
  }
  catch(ThreadInterruptedException ex)
  {
   Console.WriteLine("I value is {0}",i);
   Console.WriteLine("Interrupted:{0}",ex.Message);
  }
  finally
  {
   Console.WriteLine("In Finally State");
  }
 }
}

参与评论 您还未登录,请先 登录 后发表或查看评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

wujevons

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值