Montior实现加锁

复制代码
public class MonitorThreadTest
{
    static int count =0 ;
    public static void Run(){
        Stopwatch stopwatch = Stopwatch.StartNew();
        Thread thread1 = new Thread(new ThreadStart(AddCount));
        Thread thread2 = new Thread(new ThreadStart(AddCount));
        thread1.Start();
        thread2.Start();
        thread1.Join();
        thread2.Join();
        stopwatch.Stop();
        System.Console.WriteLine($"总数:{count},时间:{stopwatch.Elapsed}毫秒");
    }
    private static Object lockObj = new object();
    public static void AddCount(){
        Monitor.Enter(lockObj);
        for(int i=0; i<1000000; i++){
            count++;
        }
        Monitor.Exit(lockObj);
    }
}
复制代码

 

posted @   vba是最好的语言  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
历史上的今天:
2022-11-28 vba ad域服务器信息怎么获取,获取AD域用户登入信息
2021-11-28 记一下EF code Frist错误
点击右上角即可分享
微信分享提示