• 多线程lock(this.lockObject)的作用验证 Anny


    namespace MultiThread
    {
        class SynchronizationThreadsExample
        {
            private int counter = 0; //被多个线程共享
            private object lockObject = new object();
            static void Main(string[] args)
            {
                SynchronizationThreadsExample STE = new SynchronizationThreadsExample();
                STE.ThreadFunction();
                Console.ReadLine();
            }
            public void ThreadFunction()
            {
                Thread DummyThread = new Thread(new ThreadStart(SomeFunction));
                DummyThread.IsBackground = true;
                DummyThread.Name = "First Thread";
                DummyThread.Start();
                Console.WriteLine("Started thread {0}", DummyThread.Name);
                Thread DummyPriorityThread = new Thread(new ThreadStart(SomeFunction));
                DummyPriorityThread.IsBackground = true;
                DummyPriorityThread.Name = "Second Thread";
                DummyPriorityThread.Start();
                Console.WriteLine("Started thread {0}", DummyPriorityThread.Name);
                DummyThread.Join();
                DummyPriorityThread.Join();
            }
            public void SomeFunction()
            {
                try
                {
                    while (counter < 10)
                    {
                            counter++;
                            Thread.Sleep(1);
                            Console.WriteLine("Thread . SomeFunction-counter: " + Thread.CurrentThread.Name + counter);               

          }
                }
                catch (ThreadInterruptedException Ex)
                {
                    Console.WriteLine("Exception in thread " + Thread.CurrentThread.Name);
                }
                finally
                {
                    Console.WriteLine("Thread Exiting. " + Thread.CurrentThread.Name);
                }
            }
        }
    }

    运行结果:

    Started thread First Thread
    Thread . SomeFunction-counter: First Thread1
    Thread . SomeFunction-counter: First Thread2
    Thread . SomeFunction-counter: First Thread3
    Started thread Second Thread
    Thread . SomeFunction-counter: First Thread4
    Thread . SomeFunction-counter: First Thread5
    Thread . SomeFunction-counter: First Thread7
    Thread . SomeFunction-counter: Second Thread7
    Thread . SomeFunction-counter: First Thread9
    Thread . SomeFunction-counter: Second Thread9
    Thread Exiting. Second Thread
    Thread . SomeFunction-counter: First Thread10
    Thread Exiting. First Thread

    将上边黄色代码加锁,具体如下,结果将发生变化:

           lock(this.lockObject)
                       {
                            counter++;
                            Thread.Sleep(1);
                            Console.WriteLine("Thread . SomeFunction-counter: " + Thread.CurrentThread.Name + counter);
                       }

    运行结果如下:

    Started thread First Thread
    Thread . SomeFunction-counter: First Thread1
    Thread . SomeFunction-counter: First Thread2
    Started thread Second Thread
    Thread . SomeFunction-counter: First Thread3
    Thread . SomeFunction-counter: First Thread4
    Thread . SomeFunction-counter: Second Thread5
    Thread . SomeFunction-counter: Second Thread6
    Thread . SomeFunction-counter: Second Thread7
    Thread . SomeFunction-counter: Second Thread8
    Thread . SomeFunction-counter: Second Thread9
    Thread . SomeFunction-counter: First Thread10
    Thread Exiting. First Thread
    Thread . SomeFunction-counter: Second Thread11
    Thread Exiting. Second Thread

  • 相关阅读:
    LeetCode42 接雨水(单调栈)
    LeetCode198 打家劫舍
    LeetCode357 统计各位数字都不同的数字个数
    LeetCode319 灯泡开关
    LeetCode46 全排列
    Python 二叉树遍历方式总结
    Leetcode的简单算法题:69. x 的平方根
    Leetcode的SQL题:1527. 患某种疾病的患者
    Leetcode的SQL题:1965. 丢失信息的雇员
    Leetcode的SQL题:182. 查找重复的电子邮箱
  • 原文地址:https://www.cnblogs.com/limei/p/1839622.html
Copyright © 2020-2023  润新知