例子是比较简单,运行之后的结果往往比较随机的!只需要把运行的次数调高点就可!注意ParameterizedThreadStart这个参数是Object类型的!从这个例子可看出,这些线程是杂乱的运行的!只要将主线程结束,整个应用程序就会结束,而结束其他线程则不会!
请看下面的例子,
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading;
6
7 namespace ConsoleApplication3
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 Thread t = new Thread(PrintX);
14 t.Start();
15 Thread.CurrentThread.Name = "Main";
16 Console.WriteLine("主线程的应用域{0},ID为{1}", Thread.GetDomain().ToString(), Thread.GetDomainID());
17 Console.WriteLine("主线程的名字为{0},HashCode为{1}", Thread.CurrentThread.Name, Thread.CurrentThread.GetHashCode());
18
19 }
20 static void PrintX ()
21 {
22 Thread.CurrentThread.Name = "t1";
23 Console.WriteLine("t1线程的应用域{0},ID为{1}", Thread.GetDomain().ToString(), Thread.GetDomainID());
24 Console.WriteLine("t1线程的名字为{0},HashCode为{1}", Thread.CurrentThread.Name, Thread.CurrentThread.GetHashCode());
25 }
26 }
27 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading;
6
7 namespace ConsoleApplication3
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 Thread t = new Thread(PrintX);
14 t.Start();
15 Thread.CurrentThread.Name = "Main";
16 Console.WriteLine("主线程的应用域{0},ID为{1}", Thread.GetDomain().ToString(), Thread.GetDomainID());
17 Console.WriteLine("主线程的名字为{0},HashCode为{1}", Thread.CurrentThread.Name, Thread.CurrentThread.GetHashCode());
18
19 }
20 static void PrintX ()
21 {
22 Thread.CurrentThread.Name = "t1";
23 Console.WriteLine("t1线程的应用域{0},ID为{1}", Thread.GetDomain().ToString(), Thread.GetDomainID());
24 Console.WriteLine("t1线程的名字为{0},HashCode为{1}", Thread.CurrentThread.Name, Thread.CurrentThread.GetHashCode());
25 }
26 }
27 }
这个例子运行如下图所示,
这里,通Thread的一些静态属性可以获得我们一些想要的东西!这一点在平时用的也比较多!
好了,今天就写了这么多了!