首先lock和Minitor有什么区别呢?
其实lock在IL代码中会被翻译成Monitor。也就是Monitor.Enter(obj)和Monitor.Exit(obj).
lock(obj)
{
}
等价为:
try
{   
      Monitor.Enter(obj)

}
catch()
{}
finally
{
      Monitor.Exit(obj)

}
所以lock能做的,Monitor肯定能做,Monitor能做的,lock不一定能做。那么Monitor额外的功能呢?
1:Monitor.TryEnter(obj,timespan)----timeout之后,就不执行这段代码了。lock可是一直会死等的。

2:还有Monitor.Wait()和Monitor.Pulse()。在lock代码里面如果调用了Monitor.Wait(),会放弃对资源的所有权,让别的线程lock进来。然后别的线程代码里Pulse一下(让原线程进入到等待队列),然后在Wait一下释放资源,这样原线程的就可以继续执行了(代码还堵塞在wait那句话呢)。
也就是说,必须两个或多个线程共同调用Wait和Pulse,把资源的所有权抛来抛去,才不会死锁。

和AutoEvent相似是处理同步关系的,但是AutoEvent是跨进程的,而Monitor是针对线程的。
以下是MSDN的代码示例,调试起来很容易看出来两个函数的作用了,因为尽管是多线程程序,但是是同步操作,所以代码始终是单步执行的。

using System;
using System.Threading;
using System.Collections;

namespace MonitorCS1
{
    class
MonitorSample
    {
       
const int MAX_LOOP_TIME = 100;
       
Queue m_smplQueue;

public MonitorSample()
       
{
           
m_smplQueue = new Queue();
       
}
       
public void FirstThread()
       
{
           
int counter = 0;
           
lock (m_smplQueue)
           
{
               
while (counter < MAX_LOOP_TIME)
               
{
                   
//Wait, if the queue is busy.
                   
Monitor.Wait(m_smplQueue);
                   
//Push one element.
                   
m_smplQueue.Enqueue(counter);
                   
//Release the waiting thread.
                   
Monitor.Pulse(m_smplQueue);

counter++;
               
}
               
int i = 0;
           
}
       
}
       
public void SecondThread()
       
{
           
lock (m_smplQueue)
           
{
               
//Release the waiting thread.
               
Monitor.Pulse(m_smplQueue);
               
//Wait in the loop, while the queue is busy.
               
//Exit on the time-out when the first thread stops.
               
while (Monitor.Wait(m_smplQueue, 50000))
               
{
                   
//Pop the first element.
                   
int counter = (int)m_smplQueue.Dequeue();
                   
//Print the first element.
                   
Console.WriteLine(counter.ToString());
                   
//Release the waiting thread.
                   
Monitor.Pulse(m_smplQueue);
               
}
           
}
       
}
       
//Return the number of queue elements.
       
public int GetQueueCount()
       
{
           
return m_smplQueue.Count;
       
}

static void Main(string[] args)
       
{
           
//Create the MonitorSample object.
           
MonitorSample test = new MonitorSample();
           
//Create the first thread.
           
Thread tFirst = new Thread(new
ThreadStart(test.FirstThread));
           
//Create the second thread.
           
Thread tSecond = new Thread(new
ThreadStart(test.SecondThread));
           
//Start threads.
           
tFirst.Start();
           
tSecond.Start();
           
//wait to the end of the two threads
           
tFirst.Join();
           
tSecond.Join();
           
//Print the number of queue elements.
           
Console.WriteLine("Queue Count = " +
test.GetQueueCount().ToString());
       
}
    }
}

c#中Monitor的使用的更多相关文章

  1. C#中Monitor对象与Lock关键字的区别分析

    这篇文章主要介绍了C#中Monitor对象与Lock关键字的区别,需要的朋友可以参考下 Monitor对象 1.Monitor.Enter(object)方法是获取 锁,Monitor.Exit(ob ...

  2. C#知识点总结系列:4、C#中Monitor和Lock以及区别

    Monitor对象 1.Monitor.Enter(object)方法是获取锁,Monitor.Exit(object)方法是释放锁,这就是Monitor最常用的两个方法,当然在使用过程中为了避免获取 ...

  3. C#中Monitor类、Lock关键字和Mutex类

    线程:线程是进程的独立执行单元,每一个进程都有一个主线程,除了主线程可以包含其他的线程.多线程的意义:多线程有助于改善程序的总体响应性,提高CPU的效率.多线程的应用程序域是相当不稳定的,因为多个线程 ...

  4. C#中Monitor和Lock以及区别

    Monitor对象 1.Monitor.Enter(object)方法是获取锁,Monitor.Exit(object)方法是释放锁,这就是Monitor最常用的两个方法,当然在使用过程中为了避免获取 ...

  5. 锁、C#中Monitor和Lock以及区别

    1.Monitor.Enter(object)方法是获取锁,Monitor.Exit(object)方法是释放锁,这就是Monitor最常用的两个方法,当然在使用过程中为了避免获取锁之后因为异常,致锁 ...

  6. 转:C#中Monitor对象与Lock关键字的区别分析

    Monitor对象1.Monitor.Enter(object)方法是获取 锁,Monitor.Exit(object)方法是释放锁,这就是Monitor最常用的两个方法,当然在使用过程中为了避免获取 ...

  7. 关于android sdk中monitor.exe报错的问题

    今天又是被坑的一上午.来总结一下: 1. 首先是找不到monitor的问题: 这个可能是一开始环境配置错误.所以我将android sdk重装了一下就好了 2. 第二个是找到monitor.bat发现 ...

  8. 【C#】【Thread】Monitor和Lock

    所谓锁,就是之锁定的区域只能单个线程进入进行操作,其他线程在锁的外围等待.Monitor锁通过Monitor.Enter(obj)和Monitor.Exit(obj)来锁定和解锁.Lock锁则直接Lo ...

  9. linux中set的用法

    功能说明:设置shell.语 法:set [+-abCdefhHklmnpPtuvx]补充说明:用set 命令可以设置各种shell选项或者列 出shell变量.单个选项设置常用的特性.在某些选项之后 ...

随机推荐

  1. Maven 的基本用法

    一.Maven 的安装 二.Maven的常用构建命令 1.mvn -v 查看maven版本 2.mvn compile 编译 3.mvn test 测试 4.mvn package 打包 5.Mvn  ...

  2. hibernate的各个jar包的作用

    hibernate的各个jar包的作用 最基本的Hibernate3.3.2之 JAR包(必要): 包名 位置 用途 hibernate3.jar /hibernate 核心JAR包 antlr.ja ...

  3. cf 512D - Fox And Travelling

    题目大意 给定一颗\(n\le 100\)个点的图,可以进行随机游走,求游走\(k=0...n\)个点的方案数 游走的规则是:每次只能访问一个度数\(\le 1\)的点,并将其删除 分析 看完傻眼 问 ...

  4. Session 存储和失效方式

    Session 一般的操作是放在本地的Asp.net StatService上.实现进程隔离,方便Session操作,下面说说Session各属性. 1)不使用Session <sessionS ...

  5. 【Codeforces Round #518 (Div. 2)】

    A:https://www.cnblogs.com/myx12345/p/9847588.html B:https://www.cnblogs.com/myx12345/p/9847590.html ...

  6. VIjosP1046观光旅游

    背景 湖南师大附中成为百年名校之后,每年要接待大批的游客前来参观.学校认为大力发展旅游业,可以带来一笔可观的收入. 描述 学校里面有N个景点.两个景点之间可能直接有道路相连,用Dist[I,J]表示它 ...

  7. 全球主要城市经纬度api

    原文发布时间为:2011-06-23 -- 来源于本人的百度文章 [由搬家工具导入] http://www.google.com/ig/cities?country=cn http://www.goo ...

  8. 恶补一下DP+背包专题(刷刷水题)L2

    开心的金明 题目大意 就是求一定背包容量的最大值 思路 想必大家都知道,一看到这种题目,就会想起01背包 虽然特别简单但是还是讲一下吧 状态设置 由于这题差不多是一个01背包的版子题,那么我们就只需要 ...

  9. @RequestMapping定义不同的处理器映射规则

    通过@RequestMapping注解可以定义不同的处理器映射规则. 1. URL路径映射 @RequestMapping(value="item")或@RequestMappin ...

  10. 转 网络编程学习笔记一:Socket编程

    题外话 前几天和朋友聊天,朋友问我怎么最近不写博客了,一个是因为最近在忙着公司使用的一些控件的开发,浏览器兼容性搞死人:但主要是因为这段时间一直在看html5的东西,看到web socket时觉得很有 ...