线程同步锁lock(object) 与Monitor.enter(object)的使用

3 篇文章 0 订阅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication3
{
    class Program
    {
        static long Sum=0;
        static object mylock;
        static void Main(string[] args)
        {
            long n = 0;
            long s1 = DateTime.Now.Ticks;
            for (int i = 0; i <= 100; i++)
            {
                n += i;
                Thread.Sleep(10); 
            }
            long s2 = DateTime.Now.Ticks;
            Console.Write("----------------单线程运行-------------------\nsum:" + n + "\n\r所花时间(纳秒):" + (s2 - s1)+"\n");
            Console.Write("----------------双线程运行-------------------\n");
            Thread t1 = new Thread(add1);
            Thread t2 = new Thread(add2);
            mylock = new object();
            s1 = DateTime.Now.Ticks;
            t1.Start();
            t2.Start();
            t1.Join();
            t2.Join();
            s2 = DateTime.Now.Ticks;
            Console.Write("sum:" + n + "\n\r所花时间(纳秒):" + (s2 - s1));
            Console.ReadKey();
        }
        static void add1()
        {
            long n = 0;
            for (int i = 0; i <= 50; i++)
            {
                n += i;
                Thread.Sleep(10);
            }
            Monitor.Enter(mylock);
            Sum += n;
            Console.Write("线程一运行完毕,Sum:" + Sum+"\n");
            Monitor.Exit(mylock);
           
            
        }
        static void add2()
        {
            long n = 0;
            for (int i = 51; i <= 100; i++)
            {
                n += i;
                Thread.Sleep(10);
            }
            Monitor.Enter(mylock);
            Sum += n;
            Console.Write("线程二运行完毕,Sum:" + Sum + "\n");
            Monitor.Exit(mylock);
           
        }
    }
}
Lock(object)锁的使用
using System;
using System.Threading;
namespace program
{
    class wangjun
    {
        public static string buff = "0";
        public const int ab = 1000000;
        private object mylock = new object();
        static void Main(string[] args)
        {
            wangjun wj = new wangjun();
            Thread th = new Thread(new ThreadStart(wj.xuan1));
            th.Start();
            Thread th2 = new Thread(new ThreadStart(wj.xuan2));
            th2.Start();
            th.Join();
            th2.Join();
            Console.WriteLine("结果是:{0}",buff);
        }
        public void xuan1()
        {
            for (int i = 0; i < ab/2; i++)
            {
                lock (mylock)
                {
                    buff = (long.Parse(buff) + i).ToString();
                }
            }
        }
        public void xuan2()
        {
            for (int i = ab/2; i <= ab; i++)
            {
                lock (mylock)
                {
                    buff = (long.Parse(buff) + i).ToString();
                }
            }
        }
    }
}
Monitor.enter(object)的使用
using System;
using System.Threading;
namespace program
{
    class wangjun
    {
        public static string buff = "0";
        public const int ab = 1000000;
        private object mylock = new object();
        static void Main(string[] args)
        {
            wangjun wj = new wangjun();
            Thread th = new Thread(new ThreadStart(wj.xuan1));
            th.Start();
            Thread th2 = new Thread(new ThreadStart(wj.xuan2));
            th2.Start();
            th.Join();
            th2.Join();
            Console.WriteLine("结果是:{0}",buff);
        }
        public void xuan1()
        {
            for (int i = 0; i < ab/2; i++)
            {
                Monitor.Enter(mylock);
                try
                {
                    buff = (long.Parse(buff) + i).ToString();
                }
                finally 
                {
                    Monitor.Exit(mylock);
                }
            }
        }
        public void xuan2()
        {
            for (int i = ab/2; i <= ab; i++)
            {
                Monitor.Enter(mylock);
                try
                {
                    buff = (long.Parse(buff) + i).ToString();
                }
                finally
                {
                    Monitor.Exit(mylock);
                }
            }
        }
    }
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值