Skip to content

使用 lock 实现线程同步

通过 lock 一个私有的引用成员变量来完成成员方法内的线程同步;

通过 lock 一个私有的静态引用成员变量来完成静态方法内的线程同步;

示例代码

Lock.cs

点击查看代码
cs
using System;
using System.Threading;

namespace UseLock
{
    class Lock
    {
        /// <summary>
        /// 用来在静态方法中同步
        /// </summary>
        private static object o1 = new object();
        /// <summary>
        /// 用来在成员方法中同步
        /// </summary>
        private object o2 = new object();
        /// <summary>
        /// 静态变量
        /// </summary>
        private static int i1 = 0;
        /// <summary>
        /// 成员变量
        /// </summary>
        private int i2 = 0;

        /// <summary>
        /// 测试静态方法的同步
        /// </summary>
        /// <param name="state"></param>
        public static void Increment1(object state)
        {
            lock (o1)
            {
                Console.WriteLine("i1 的值为:{0}", i1.ToString());
                // 这里刻意制造线程并行机会,来检查同步功能
                Thread.Sleep(200);
                i1++;
                Console.WriteLine("i1 自增后为:{0}", i1.ToString());
            }
        }

        /// <summary>
        /// 测试成员方法的同步
        /// </summary>
        /// <param name="state"></param>
        public void Increment2(object state)
        {
            lock (o2)
            {
                Console.WriteLine("i2 的值为:{0}", i2.ToString());
                // 这里刻意制造线程并行机会,来检查同步功能
                Thread.Sleep(200);
                i2++;
                Console.WriteLine("i2 自增后为:{0}", i2.ToString());
            }
        }
    }
}

Program.cs

点击查看代码
cs
using System;
using System.Threading;

namespace UseLock
{
    class Program
    {
        static void Main(string[] args)
        {
            // 开始多线程
            Console.WriteLine("开始测试静态方法的同步");
            for (int i = 0; i < 5; i++)
            {
                Thread t = new Thread(Lock.Increment1);
                t.Start();
            }
            // 等待线程执行结束
            Thread.Sleep(5 * 1000);

            Console.WriteLine("开始测试成员方法的同步");
            Lock l = new Lock();
            for (int i = 0; i < 5; i++)
            {
                Thread t = new Thread(l.Increment2);
                t.Start();
            }
            Console.Read();
        }
    }
}

输出结果

点击查看输出结果
开始测试静态方法的同步
i1的值为:0
i1自增后为:1
i1的值为:1
i1自增后为:2
i1的值为:2
i1自增后为:3
i1的值为:3
i1自增后为:4
i1的值为:4
i1自增后为:5
开始测试成员方法的同步
i2的值为:0
i2自增后为:1
i2的值为:1
i2自增后为:2
i2的值为:2
i2自增后为:3
i2的值为:3
i2自增后为:4
i2的值为:4
i2自增后为:5

lock 删除掉后的执行结果

点击查看输出结果
开始测试静态方法的同步
i1的值为:0
i1的值为:0
i1的值为:0
i1的值为:0
i1的值为:0
i1自增后为:1
i1自增后为:5
i1自增后为:3
i1自增后为:2
i1自增后为:4
开始测试成员方法的同步
i2的值为:0
i2的值为:0
i2的值为:0
i2的值为:0
i2的值为:0
i2自增后为:1
i2自增后为:2
i2自增后为:3
i2自增后为:1
i2自增后为:4

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.