0 .查看当前的线程信息:Thread.CurrentThread.属性
1 private static Thread subthread ;
2 private static Thread subthread1;
3 static void Main(
string[] args)
4 {
5 Thread.CurrentThread.Name =
"Main线程";
6 Console.WriteLine(Thread.CurrentThread.Name);
7 //开启了一个新的线程
8 subthread =
new Thread(
new ThreadStart(GetShow));
//无参数的入口方法线程
9 subthread.Start();
//开启线程
10 subthread.Name =
"无参数的入口方法线程";
11 //又开启了一个线程
12 subthread1 =
new Thread(
new ParameterizedThreadStart(GetShow));
//有参数的入口方法线程
13 subthread1.Start(
"实际参数");
//开启线程传参
14 subthread1.Name =
"有参数的入口方法线程";
15
16 Console.WriteLine(
"主线程结束");
17 }
18
19 static void GetShow()
20 {
21
22 Console.WriteLine(Thread.CurrentThread.Name);
23 Console.WriteLine(
"执行无参数的方法");
24 }
25
26 static void GetShow(
object obj)
27 {
28 Console.WriteLine(Thread.CurrentThread.Name);
29 Console.WriteLine(
"执行有参数的方法,传参为:" + (
string)obj);
30 }
31 }
View Code
一. Sleep()阻塞线程休息方法的使用情况:
Thread.Sleep()方法用于使当前线程暂停指定的时间,然后去执行流程语句。
他的意思是,你可以让某个线程先“谢谢”,让其他的兄弟先干活,他歇息“一会儿”后再干活
1 // 参数:
2 // millisecondsTimeout:
3 // 线程被阻塞的毫秒数。 指定零 (0) 以指示应挂起此线程以使其他等待线程能够执行。 指定 System.Threading.Timeout.Infinite
4 // 以无限期阻止线程。
5 //
6 public static void Sleep(
int millisecondsTimeout);
7 // 参数:
8 // timeout:
9 // 设置为线程被阻塞的时间量的 System.TimeSpan。 指定零以指示应挂起此线程以使其他等待线程能够执行。 指定 System.Threading.Timeout.Infinite
10 // 以无限期阻止线程。
11 public static void Sleep(TimeSpan timeout);
View Code
常用于需要开启一个不停执行某个操作,进行循环时候,需要对Thread进行阻塞,减少计算机资源的消耗,和合理的控制程序逻辑的执行。
1 private static Thread subthread ;
2 static void Main(
string[] args)
3 {
4 Thread.CurrentThread.Name =
"Main线程";
5 //无参数的入口方法线程;原本的结果哈,是争相恐后的干活,现在
6 //GetShow的这个方法里先“等”10s,等过了10s再执行数据。
7 //subthread相当于“卡”了10s
8 subthread =
new Thread(
new ThreadStart(GetShow));
9 subthread.Start();
//开启线程
10 subthread.Name =
"无参数的入口方法线程";
11
12 Console.WriteLine(
"主线程结束");
13 }
14
15 static void GetShow()
16 {
17 while (
true)
18 {
19 Console.WriteLine(Thread.CurrentThread);
20 Console.WriteLine(
"进行数据同步");
21 Thread.Sleep(
10000);
//阻塞10000进行一次数据的同步
22 }
23 }
View Code
二. Interrupt() 唤醒休眠中的线程的方法
Interrupt这个词的意思是 中断;打断;插嘴;妨碍 | vi. 打断...
1 class Program
2 {
3 private static Thread subthread ;
4 static void Main(
string[] args)
5 {
6 Thread.CurrentThread.Name =
"Main线程";
7 //无参数的入口方法线程 在方法里睡了十秒;
8 //但是在他睡的十秒的同时,主线程还在继续工作呀
9 //就继续执行下面的for循环,但是for循环每次输入1s
10 //一共就用了5s;
11 //应为程序是从上而下读取的
12 //所以下一步就调用的 subthread.Interrupt();这个方法
13 //但是,刺客的 subthread这个线程才睡了5s,所以就会被唤醒,然后结束
14 subthread =
new Thread(
new ThreadStart(GetShow));
15 subthread.Name =
"无参数的入口方法线程";
16 subthread.Start();
//开启线程
17 for (
int i =
1; i <=
5; i++
)
18 {
19 Thread.Sleep(
1000);
20 Console.WriteLine(
"第{0}秒",i);
21 }
22 subthread.Interrupt();
23
24 Console.WriteLine(
"主线程结束");
25 }
26
27 static void GetShow()
28 {
29 Console.WriteLine(
"名称"+
Thread.CurrentThread.Name);
30 try
31 {
32 Console.WriteLine(
"休息十秒");
33 Thread.Sleep(
10000);
34 }
35 catch (Exception ex)
36 {
37 Console.WriteLine(
"线程被唤醒,只休息了5秒");
38 Console.WriteLine(ex.Message);
39 }
40 Console.WriteLine(Thread.CurrentThread.Name);
41 }
42 }
View Code
注意:
线程在休眠的那一刻,被唤醒时,Thread.Sleep()方法会抛出异常,所有要用 try { }catch (Exception ex){ }包起来;
线程在没有休眠的那一刻被唤醒,在后面某一端时间休眠后Thread.Sleep()方法会抛出异常,所有进行 try { }catch (Exception ex){ }异常处理,避免程序死掉。
有点扭嘴,意思就是try catch 避免程序死掉。
转载于:https://www.cnblogs.com/ZkbFighting/p/9030514.html