ThreadPriority.Highest ThreadPriority.AboveNormal ThreadPriority.Normal ThreadPriority.BelowNormal ThreadPriority.Lowest
ThreadPriority. Normal.
, , : . MyThread. Run () , . , 1000000000 stop true. stop false. , 1000000000, true stop. . currentName . , , . , , currentName. . .
// .
Using System;
Using System.Threading;
class MyThread { public int Count; public Thread Thrd;
static bool stop = false; static string currentName;
/* . , . */ public MyThread(string name) {
Count = 0;
Thrd = new Thread(this.Run);
Thrd.Name = name; currentName = name;
}
// , void Run() {
Console.WriteLine(" " + Thrd.Name + " ."); do {
Count++;
if(currentName!= Thrd.Name) { currentName = Thrd.Name;
Console.WriteLine(" " + currentName);
}.
} while(stop == false && Count < 1000000000); stop = true;
Console.WriteLine(" " + Thrd.Name + " .");
}
}
class PriorityDemo { static void Main() {
MyThread mtl = new MyThread(" "); MyThread mt2 = new MyThread(" ");
// .
mtl.Thrd.Priority = ThreadPriority.AboveNormal;
mt2.Thrd.Priority = ThreadPriority.BelowNormal;
// , mtl.Thrd.Start(); mt2.Thrd.Start();
Mtl.Thrd.Join (); mt2.Thrd.Join();
Console.WriteLine();
Console.WriteLine(" " + mtl.Thrd.Name +
" " + mtl.Count);
Console.WriteLine(" " + mt2.Thrd.Name +
" " + mt2.Count);
}
}
.
|
|
.
.
.
.
1000000000 23996334
, 98% , . , , , Windows.
- , . , , , . , , , .
, . . , . , , . , , . - , , . .
, . , . , .
#. . lock. C# , , . C# .
:
lock (lockObj) {
//
}
lockObj . , . lock , , , , . , . .
|
|
, . , . , , , , , . lock (this). , this . , lock (this), . , . , . C# lock (this). , .