1 使用Monitor來同步
Monitor類提供兩個靜態方法Monitor.Enter()和Monitor.Exit()來保證代碼的同步。有一點要保證的是調用了Monitor.Enter()後必須有一個相對應的Monitor.Exit()。所以,一個很好的做法是將這兩個方法用一個Try/Finally塊包圍起來。
示例:
class Program
{
public const int _total = 100000;
//聲明爲readonly防止在其他地方被修改
readonly static object _sync = new object();
static long _count = 0;
static void Main(string[] args)
{
ThreadStart threadStart =new ThreadStart(DoWork);
Thread thread = new Thread(DoWork);
thread.Start();
Thread t = Thread.CurrentThread;
t.Name = "Main thread";
for (int i = 0; i < _total; i++)
{
try
{
Monitor.Enter(_sync);
//Console.Write("+");
_count++;
}
finally
{
Monitor.Exit(_sync);
}
}
thread.Join();
Console.WriteLine(t.Name+"has finish");
Console.WriteLine("Count:" + _count);
Console.Read();
}
public static void DoWork()
{
Thread.CurrentThread.Name = " NewThread ";
for (int i = 0; i < _total; i++)
{
try
{
Monitor.Enter(_sync);
_count--;
//Console.Write("-");
}
finally
{
Monitor.Exit(_sync);
}
}
Console.WriteLine(Thread.CurrentThread.Name + "has finish");
Console.WriteLine("Count:" + _count);
}
}
輸出:
Count爲0.
若把Monitor.Enter()和Monitor.Exit()註釋掉的話,Count就不一定爲0,特別是循環次數_total 特別大的時候。
Monitor類有一個缺點就是對性能影響很大。方法中有使用Monitor.Enter()和沒有使用執行時間相差很大。
2 使用Lock關鍵字
如果你覺得Monitor要既要寫Enter()又要調用Exit()很麻煩,那你可以使用Lock()關鍵字。並且使用Lock()的性能會比Monitor好一點點。
class Program
{
public const int _total = 100000;
//聲明爲readonly防止在其他地方被修改
readonly static object _sync = new object();
static long _count = 0;
static void Main(string[] args)
{
ThreadStart threadStart =new ThreadStart(DoWork);
Thread thread = new Thread(DoWork);
thread.Start();
Thread t = Thread.CurrentThread;
t.Name = "Main thread";
for (int i = 0; i < _total; i++)
{
lock (_sync)
{
_count++;
}
}
thread.Join();
Console.WriteLine(t.Name+"has finish");
Console.WriteLine("Count:" + _count);
Console.Read();
}
public static void DoWork()
{
Thread.CurrentThread.Name = " NewThread ";
for (int i = 0; i < _total; i++)
lock (_sync)
{
_count--;
}
}
Console.WriteLine(Thread.CurrentThread.Name + "has finish");
Console.WriteLine("Count:" + _count);
}
}
3 MethodImplAttribute類
MethodImplAttribute會告訴CLR方法是如何實現的,其屬性設置爲MethodImplOptions.Synchronized時,它指定在任一時刻只允許一個線程訪問這個方法。
class MethodImpi
{
[MethodImpl(MethodImplOptions.Synchronized)]
public static void DoSomeWorkSync()
{
Thread.CurrentThread.Name="Sync"+DateTime.Now.Ticks;
Console.WriteLine("DoSomeWorkSync() --Lock by Thread" + Thread.CurrentThread.GetHashCode());
Thread.Sleep(5 * 1000);
Console.WriteLine("DoSomeWorkSync() --Released by Thread" + Thread.CurrentThread.GetHashCode());
}
public static void DoSomeWorkNoSync()
{
Thread.CurrentThread.Name = "Sync" + DateTime.Now.Ticks;
Console.WriteLine("DoSomeWorkNoSync() --Lock by Thread" + Thread.CurrentThread.GetHashCode());
Thread.Sleep(5 * 1000);
Console.WriteLine("DoSomeWorkNoSync() --Released by Thread" + Thread.CurrentThread.GetHashCode());
}
static void Main(string[] args)
{
Thread th1 = new Thread(DoSomeWorkSync);
Thread th2 = new Thread(DoSomeWorkSync);
th1.Start();
th2.Start();
Thread th3 = new Thread(DoSomeWorkNoSync);
Thread th4 = new Thread(DoSomeWorkNoSync);
th3.Start();
th4.Start();
}
}
結果: