您的位置:首页 > 编程语言 > C#

C#中的Thread中的ApartmentState几种状态(STA,MTA,Unknown)详解

2011-12-21 11:28 603 查看
System.Threading命名空间是.Net多线程编程的基础。对于多线程编程在实际工作中一直用的不多,所以了解也就不多。尽管如此,随着多核,多个cpu的出现,大计算量的需要,多线程将越来越受关注。所以打算写个系列博客,以便更多的了解学习多线程的知识。听说.Net4.0中有一个更方便多线程的类库,可惜还没真的见识过,先熟悉System.Threading就当“温故而知新”了

从Thread的线程单元状态ApartmentState说起

ApartmentState是一个枚举变量,用来设置线程的单元状态(单元状态的ApartmentState的中文msdn翻译,这个翻译很水,我不能从这四个汉字中确切的了解英文ApartmentState要表达的意思)。ApartmentState有三个枚举值,分别为STA:表示Thread将被创建并进入一个单线程单元,我猜想STA应该是Single Thread Apartment的首字母简拼;MTA:表示Thread将被创建并进入一个多线程单元,还有一个是Unknown,表示没有设置线程的单元状态。我在以前使用Thread的时候,从来没有设置过线程的单元状态,今天要做个试验把这三种状态搞清楚。

使用新new 一个Thread实例之后可以使用SetAppartmentState方法设置线程的单元状态,每个线程只可以设置一次,若再次设置会抛异常,若不知道是否设置了单元状态可以使用Thread类提供的TrySetApartmentState方法来设置;不设置时其线程单元在控制台应用程序中默认是MTA。

试验思路:

1. 使用new 3个Thread的实例,什么都不执行,看两种不同的AppartmentState的Thread的执行顺序如何

2. 同样new 3个Thread实例,执行一段计算代码,看两种不同的AppartmentState执行完全部计算耗时情况

具体的实验代码如下:

C#代码

1.using System;

2.using System.Collections.Generic;

3.using System.Text;

4.using System.Threading;

5.using System.Diagnostics;

6.using System.Collections;

7.

8.namespace MutiThread

9.{

10. class Program

11. {

12. static Stopwatch swAll;

13. static Random r = new Random();

14. static Hashtable hashTable;

15.

16.

17. static void Main(string[] args)

18. {

19. Start3Thread(ApartmentState.STA);

20.

21. Console.ReadLine();

22. }

23.

24. static void Start3Thread(ApartmentState appartmentState)

25. {

26. int threadCn = 3;

27. hashTable = new Hashtable();

28. swAll = new Stopwatch();

29. swAll.Start();

30.

31. do

32. {

33. StartThread(appartmentState);

34. threadCn--;

35. } while (threadCn > 0);

36. }

37.

38. static void StartThread(ApartmentState appartmentState)

39. {

40. Thread t1 = new Thread(new ThreadStart(CalcSomething));

41.

42. //Thread t1 = new Thread(new ThreadStart(DoNothing));

43. hashTable.Add(t1.ManagedThreadId, false);

44. t1.SetApartmentState(appartmentState);

45. t1.Start();

46. }

47.

48.

49.

50. static void CalcSomething()

51. {

52. Stopwatch sw = new Stopwatch();

53. sw.Start();

54. int[] arr = new int[1000];

55. for (int i = 0; i < arr.Length; i++)

56. {

57. arr[i] = r.Next(1000);

58. }

59. //Console.WriteLine("线程" + Thread.CurrentThread.ManagedThreadId

60. // + "的单元状态是:" + Thread.CurrentThread.GetApartmentState()

61. // + ";线程状态:" + Thread.CurrentThread.ThreadState

62. // + ";耗时:" + sw.ElapsedTicks);

63. hashTable[Thread.CurrentThread.ManagedThreadId] = true;

64.

65. if (hashTable.Count == 3)

66. {

67. bool allFinish = true;

68. foreach (object key in hashTable.Keys)

69. {

70. allFinish = allFinish && (bool)hashTable[key];

71. }

72. if (allFinish)

73. {

74. swAll.Stop();

75. Console.WriteLine(Thread.CurrentThread.GetApartmentState().ToString() + "总耗时:" + swAll.ElapsedTicks);

76. if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)

77. {

78. hashTable.Clear();

79. swAll.Reset();

80. swAll.Start();

81. Start3Thread(ApartmentState.MTA);

82. }

83. }

84. }

85. }

86.

87. static void DoNothing()

88. {

89. int times = 3;

90. do

91. {

92. Console.WriteLine("线程" + Thread.CurrentThread.ManagedThreadId

93. + "的单元状态是:" + Thread.CurrentThread.GetApartmentState()

94. + ";线程状态:" + Thread.CurrentThread.ThreadState);

95. times--;

96. } while (times > 0);

97. }

98. }

99.}

using System;

using System.Collections.Generic;

using System.Text;

using System.Threading;

using System.Diagnostics;

using System.Collections;

namespace MutiThread

{

class Program

{

static Stopwatch swAll;

static Random r = new Random();

static Hashtable hashTable;

static void Main(string[] args)

{

Start3Thread(ApartmentState.STA);

Console.ReadLine();

}

static void Start3Thread(ApartmentState appartmentState)

{

int threadCn = 3;

hashTable = new Hashtable();

swAll = new Stopwatch();

swAll.Start();

do

{

StartThread(appartmentState);

threadCn--;

} while (threadCn > 0);

}

static void StartThread(ApartmentState appartmentState)

{

Thread t1 = new Thread(new ThreadStart(CalcSomething));

//Thread t1 = new Thread(new ThreadStart(DoNothing));

hashTable.Add(t1.ManagedThreadId, false);

t1.SetApartmentState(appartmentState);

t1.Start();

}

static void CalcSomething()

{

Stopwatch sw = new Stopwatch();

sw.Start();

int[] arr = new int[1000];

for (int i = 0; i < arr.Length; i++)

{

arr[i] = r.Next(1000);

}

//Console.WriteLine("线程" + Thread.CurrentThread.ManagedThreadId

// + "的单元状态是:" + Thread.CurrentThread.GetApartmentState()

// + ";线程状态:" + Thread.CurrentThread.ThreadState

// + ";耗时:" + sw.ElapsedTicks);

hashTable[Thread.CurrentThread.ManagedThreadId] = true;

if (hashTable.Count == 3)

{

bool allFinish = true;

foreach (object key in hashTable.Keys)

{

allFinish = allFinish && (bool)hashTable[key];

}

if (allFinish)

{

swAll.Stop();

Console.WriteLine(Thread.CurrentThread.GetApartmentState().ToString() + "总耗时:" + swAll.ElapsedTicks);

if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)

{

hashTable.Clear();

swAll.Reset();

swAll.Start();

Start3Thread(ApartmentState.MTA);

}

}

}

}

static void DoNothing()

{

int times = 3;

do

{

Console.WriteLine("线程" + Thread.CurrentThread.ManagedThreadId

+ "的单元状态是:" + Thread.CurrentThread.GetApartmentState()

+ ";线程状态:" + Thread.CurrentThread.ThreadState);

times--;

} while (times > 0);

}

}

}

实验的结果是:

1. AppartmentState为STA或者MTA时的执行顺序都是不定的,每一次执行都可能不同,也就是说顺序上无法说明两种的区别。

2. 两种不同的ApartmentState的执行效率上是有区别的,单线程单元状态模式所耗时间明显多于多线程单元模式状态

3. 在不设置线程的AppartmentState时,默认值是MTA,也就是多线程模式的

我的测试CPU是单CPU的,具体如下:

Intel(R)Pentium(R)4CPU

3.00GHz

2.99GHz,1.99GB的内存
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: