|
发表于 2020-2-9 16:15:01
|
显示全部楼层
public class T
{
public static long lNum=0;
public static void Main()
{
const int MaxThread=7;
Thread[] arrt=new Thread[7];
for (int i=0;i<MaxThread;i++)
{
arrt[i]=new Thread(new ThreadStart(Run));
arrt[i].Name=i.ToString();
arrt[i].Start();
}
}
private static object thisLock=new object();
public static void Run()
{
lock(thisLock)
{
while (lNum<100)
{
lNum++;
Console.WriteLine("Thread{0}: lNum={1}", Thread.CurrentThread.Name, lNum);
}
}
}
}
/*-----------------------------------------------
Output:
Thread0: lNum=1
Thread0: lNum=2
Thread0: lNum=3
Thread0: lNum=4
Thread0: lNum=5
Thread0: lNum=6
Thread0: lNum=7
Thread0: lNum=8
Thread0: lNum=9
Thread0: lNum=10
Thread0: lNum=11
Thread0: lNum=12
Thread0: lNum=13
Thread0: lNum=14
Thread0: lNum=15
Thread0: lNum=16
Thread0: lNum=17
Thread0: lNum=18
Thread0: lNum=19
Thread0: lNum=20
Thread0: lNum=21
Thread0: lNum=22
Thread0: lNum=23
Thread0: lNum=24
Thread0: lNum=25
Thread0: lNum=26
Thread0: lNum=27
Thread0: lNum=28
Thread0: lNum=29
Thread0: lNum=30
Thread0: lNum=31
Thread0: lNum=32
Thread0: lNum=33
Thread0: lNum=34
Thread0: lNum=35
Thread0: lNum=36
Thread0: lNum=37
Thread0: lNum=38
Thread0: lNum=39
Thread0: lNum=40
Thread0: lNum=41
Thread0: lNum=42
Thread0: lNum=43
Thread0: lNum=44
Thread0: lNum=45
Thread0: lNum=46
Thread0: lNum=47
Thread0: lNum=48
Thread0: lNum=49
Thread0: lNum=50
Thread0: lNum=51
Thread0: lNum=52
Thread0: lNum=53
Thread0: lNum=54
Thread0: lNum=55
Thread0: lNum=56
Thread0: lNum=57
Thread0: lNum=58
Thread0: lNum=59
Thread0: lNum=60
Thread0: lNum=61
Thread0: lNum=62
Thread0: lNum=63
Thread0: lNum=64
Thread0: lNum=65
Thread0: lNum=66
Thread0: lNum=67
Thread0: lNum=68
Thread0: lNum=69
Thread0: lNum=70
Thread0: lNum=71
Thread0: lNum=72
Thread0: lNum=73
Thread0: lNum=74
Thread0: lNum=75
Thread0: lNum=76
Thread0: lNum=77
Thread0: lNum=78
Thread0: lNum=79
Thread0: lNum=80
Thread0: lNum=81
Thread0: lNum=82
Thread0: lNum=83
Thread0: lNum=84
Thread0: lNum=85
Thread0: lNum=86
Thread0: lNum=87
Thread0: lNum=88
Thread0: lNum=89
Thread0: lNum=90
Thread0: lNum=91
Thread0: lNum=92
Thread0: lNum=93
Thread0: lNum=94
Thread0: lNum=95
Thread0: lNum=96
Thread0: lNum=97
Thread0: lNum=98
Thread0: lNum=99
Thread0: lNum=100
*/ |
|