多线程示例:
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
|
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 Multi-Thread..."
);
for
(
int
i = 0; i < 5; i++)
{
Thread thread =
new
Thread(Task);
thread.Start();
}
Console.Read();
}
private
static
void
Task()
{
Console.WriteLine(
string
.Format(
"Thread {0} start"
,
Thread.CurrentThread.ManagedThreadId.ToString()));
Thread.Sleep(1000);
Console.WriteLine(
string
.Format(
"Thread {0} End"
,
Thread.CurrentThread.ManagedThreadId.ToString()));
}
}
}
|
输出:
注意:线程的生成是在调用Thread的Start方法的时候.
本文转自敏捷的水博客园博客,原文链接http://www.cnblogs.com/cnblogsfans/archive/2009/11/06/1597439.html如需转载请自行联系原作者
王德水