C# 线程手册 第三章 使用线程 Monitor.TryEnter()
Monitor 類的TryEnter() 方法在嘗試獲取一個(gè)對(duì)象上的顯式鎖方面和 Enter() 方法類似。然而,它不像Enter()方法那樣會(huì)阻塞執(zhí)行。如果線程成功進(jìn)入關(guān)鍵區(qū)域那么TryEnter()方法會(huì)返回true.
TryEnter()方法的三個(gè)重載方法中的兩個(gè)以一個(gè)timeout類型值作為參數(shù),表示按照指定時(shí)間等待鎖。我們來看一個(gè)關(guān)于如何使用TryEnter()方法的例子,MonitorTryEnter.cs:
/************************************* /* Copyright (c) 2012 Daniel Dong* * Author:oDaniel Dong* Blog:o www.cnblogs.com/danielWise* Email:o guofoo@163.com* */using System; using System.Collections.Generic; using System.Text; using System.Threading;namespace MonitorTryEnter {public class TryEnter{public TryEnter(){}public void CriticalSection(){bool b = Monitor.TryEnter(this, 1000);Console.WriteLine("Thread "+ Thread.CurrentThread.GetHashCode()+ " TryEnter Value " + b);if (b){for (int i = 1; i <= 3; i++){Thread.Sleep(1000);Console.WriteLine(i + " "+ Thread.CurrentThread.GetHashCode() + " ");}}if (b){Monitor.Exit(this);}}public static void Main(){TryEnter a = new TryEnter();Thread t1 = new Thread(new ThreadStart(a.CriticalSection));Thread t2 = new Thread(new ThreadStart(a.CriticalSection));t1.Start();t2.Start();Console.ReadLine();}} }一個(gè)可能的輸出結(jié)果如下:
當(dāng)發(fā)生資源爭(zhēng)奪而你又不像讓線程睡眠一段不可預(yù)期的時(shí)間時(shí)TryEnter()方法很有用。向ISP撥號(hào)的例子很好的解釋這個(gè)。假設(shè)有兩個(gè)程序A和B,它們都想使用同一個(gè)調(diào)制解調(diào)器向ISP撥號(hào)。而一旦連接建立那么只會(huì)有一個(gè)網(wǎng)絡(luò)連接,我們不知道已有的應(yīng)用程序?qū)?huì)連接多長(zhǎng)時(shí)間。假設(shè)程序A首先向ISP撥號(hào),然后程序B也向ISP撥號(hào);毫無疑問程序B將會(huì)一直等待,因?yàn)槲覀儾恢莱绦駻將連接多久。在這種情況下,程序B可能使用TryEnter()來確定調(diào)制解調(diào)器是否已經(jīng)被另外一個(gè)應(yīng)用程序鎖定(本例中是程序A),而不是使用Enter()方法導(dǎo)致一直等待。
?
lock 關(guān)鍵字
lock 關(guān)鍵字可以作為Monitor類的一個(gè)替代。下面兩個(gè)代碼塊是等效的:
Monitor.Enter(this); //... Monitor.Exit(this);lock (this) {//... }下面的例子, Locking.cs, 使用lock 關(guān)鍵字而不是Monitor方法:
/************************************* /* copyright (c) 2012 daniel dong* * author:daniel dong* blog: www.cnblogs.com/danielwise* email: guofoo@163.com* */using System; using System.Collections.Generic; using System.Text; using System.Threading;namespace Lock {class LockWord{private int result = 0;public void CriticalSection(){lock (this){//Enter the Critical SectionConsole.WriteLine("Entered Thread "+ Thread.CurrentThread.GetHashCode());for (int i = 1; i <= 5; i++){Console.WriteLine("Result = " + result+++ " ThreadID "+ Thread.CurrentThread.GetHashCode());Thread.Sleep(1000);}Console.WriteLine("Exiting Thread "+ Thread.CurrentThread.GetHashCode());}}public static void Main(string[] args){LockWord e = new LockWord();Thread t1 = new Thread(new ThreadStart(e.CriticalSection));t1.Start();Thread t2 = new Thread(new ThreadStart(e.CriticalSection));t2.Start();//Wait till the user enters somethingConsole.ReadLine();}} }Locking.cs 的輸出與MonitorEnterExit(需要提供一個(gè)參數(shù))一樣:
?
下一篇將介紹ReaderWriterLock 類…
轉(zhuǎn)載于:https://www.cnblogs.com/danielWise/archive/2012/02/05/2339340.html
總結(jié)
以上是生活随笔為你收集整理的C# 线程手册 第三章 使用线程 Monitor.TryEnter()的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: {}企业如何才能实现多方位网络营销
- 下一篇: Java私有构造函数不能阻止继承