C# 线程手册 第三章 使用线程 Monitor.TryEnter()
Monitor 类的TryEnter() 方法在尝试获取一个对象上的显式锁方面和 Enter() 方法类似。然而,它不像Enter()方法那样会阻塞执行。如果线程成功进入关键区域那么TryEnter()方法会返回true.
TryEnter()方法的三个重载方法中的两个以一个timeout类型值作为参数,表示按照指定时间等待锁。我们来看一个关于如何使用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();
}
}
}
/* 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();
}
}
}
一个可能的输出结果如下:
当发生资源争夺而你又不像让线程睡眠一段不可预期的时间时TryEnter()方法很有用。向ISP拨号的例子很好的解释这个。假设有两个程序A和B,它们都想使用同一个调制解调器向ISP拨号。而一旦连接建立那么只会有一个网络连接,我们不知道已有的应用程序将会连接多长时间。假设程序A首先向ISP拨号,然后程序B也向ISP拨号;毫无疑问程序B将会一直等待,因为我们不知道程序A将连接多久。在这种情况下,程序B可能使用TryEnter()来确定调制解调器是否已经被另外一个应用程序锁定(本例中是程序A),而不是使用Enter()方法导致一直等待。
lock 关键字
lock 关键字可以作为Monitor类的一个替代。下面两个代码块是等效的:
Monitor.Enter(this);
//...
Monitor.Exit(this);
lock (this)
{
//...
}
//...
Monitor.Exit(this);
lock (this)
{
//...
}
下面的例子, Locking.cs, 使用lock 关键字而不是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 Section
Console.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 something
Console.ReadLine();
}
}
}
/* 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 Section
Console.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 something
Console.ReadLine();
}
}
}
Locking.cs 的输出与MonitorEnterExit(需要提供一个参数)一样:
作者:DanielWise
出处:http://www.cnblogs.com/danielWise/