.NET多线程小记(3):线程的状态

线程的状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
 
namespace MultiThreadTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Begin  Thread 1");
            Thread thread1 = new Thread(Task);
 
            Console.WriteLine("Start Thread 1");
            thread1.Start();
            PrintThreadState(thread1);
 
            Thread.Sleep(3 * 1000);
            Console.WriteLine("suspend thread1");
 
            thread1.Suspend();
            Thread.Sleep(1000);
            PrintThreadState(thread1);
 
 
            Console.WriteLine("Resume thread1");
            thread1.Resume();
            PrintThreadState(thread1);
 
            Console.WriteLine("Stop thread1");
            thread1.Abort();
            Thread.Sleep(1000);
            PrintThreadState(thread1);
 
            Console.WriteLine("Begin Thread 2");
            Thread thread2 = new Thread(Task2);
            thread2.Start();
            Thread.Sleep(2 * 1000);
            PrintThreadState(thread2);
 
            Thread.Sleep(10 * 1000);
            PrintThreadState(thread2);
            Console.Read();
 
 
        }
 
        private static void Task()
        {
            Console.WriteLine("Thread is running...");
            while (true) ;
        }
 
        private static void Task2()
        {
            Console.WriteLine("Thread start to sleep");
 
            Thread.Sleep(10 * 1000);
            Console.WriteLine("Thread was resumed");
        }
 
        private static void PrintThreadState(Thread thread)
        {
            Console.WriteLine("Thread's status is:{0}",
                thread.ThreadState.ToString());
        }
    }
}

输出

.NET多线程小记(3):线程的状态

本文转自敏捷的水博客园博客,原文链接http://www.cnblogs.com/cnblogsfans/archive/2009/11/06/1597441.html如需转载请自行联系原作者


王德水

上一篇:进程与线程


下一篇:Linux 修改yum源为国内镜像