首页 > 其他 > 详细

排他锁Lock

时间:2017-01-09 11:13:20      阅读:213      评论:0      收藏:0      [点我收藏+]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        private static object WeixinPushHelperLock = new object();
        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++)//重复执行的方法
            {
                NewMethod();
            }
        }

        private static void NewMethod()
        {
            if (Monitor.IsEntered(WeixinPushHelperLock)) return;//锁定中就return
            Monitor.Enter(WeixinPushHelperLock);
            //你的业务代码。。。

            Monitor.Exit(WeixinPushHelperLock);//执行完成后释放
        }
    }
}

 

排他锁Lock

原文:http://www.cnblogs.com/Jerrycjc/p/6264101.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!