zoukankan      html  css  js  c++  java
  • C#线程中LOCK的意义

    学习心得,为的是让新人能理解,高手直接绕~

    lock 确保当一个线程位于代码的临界区时,另一个线程不进入临界区。如果其他线程试图进入锁定的代码,则它将一直等待(即被阻止),直到该对象被释放。

    引用一句看到的资料里比较流行的话“前面说过,每个线程都有自己的资源,但是代码区是共享的,即每个线程都可以执行相同的函数。这可能带来的问题就是几个线程同时执行一个函数,导致数据的混乱,产生不可预料的结果,因此我们必须避免这种情况的发生。

    建一个控制台应用程序

    首先引用这个.....

    using System.Threading;

        class ThreadInterrupt
        {
            static void Main()
            {
                test tt = new test();
                ///写这么多个为的是让环境更加容易创造出氛围来,线程多起来,出问题的可能性就大
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
                new Thread(new ThreadStart(tt.test1)).Start();
    
    
                
            }
            class test
            {
                private int  a;
                private object b;
                public test()
                {
                    a = 8;
                    b = new object();
                }
    
                public void test1()
                {
    
    
                    //////在该位置加lock(this){
              
                    ///没有lock的情况下,所有线程都会实时进入(线程是无序的)
                        Console.WriteLine("the thread:{0} 进入 ...", Thread.CurrentThread.GetHashCode());
                    /////然后每个线程都会去判断a的值
                        if (a == 0)
                        {
                            Console.WriteLine("a={0},b={1},当前进程{2}", a, b, Thread.CurrentThread.GetHashCode());
                            return;
                        }
                    ///到此,如果线程哈希值不是4,则休眠一秒钟,为的是让晚于线程4但是已经判断a!=0的线程再次停留,给线程4充分的事件去把a置为0
                        if (Thread.CurrentThread.GetHashCode() != 4)
                        {
                            Thread.Sleep(1000);
                        }
                    /////用当前线程的哈希值除a,为的是如果a为0的话能出现异常,当然可以随便写 110/a,907867869/a都可以
                        b = (Thread.CurrentThread.GetHashCode() / a).ToString();
                    ///每个线程进入时对a做一些改变,无关紧要的
                        a -= Thread.CurrentThread.GetHashCode();
                        ///如果线程哈希值是4,则将a置为0。
                        if (Thread.CurrentThread.GetHashCode() == 4)
                        {
                            a = 0;
                        }
    
                        Console.WriteLine("a={0},b={1},当前进程{2}", a, b, Thread.CurrentThread.GetHashCode());
                    
                    //////lock的 "}"
                }
             
            }
    
        }
  • 相关阅读:
    update condition 字段报错
    Xshell连接Linux服务器总掉线
    sleep php函数
    ubuntu 16.04 镜像下载
    多线程Parallel和Task
    AngularJS 时间格式化
    正则表达式
    手机抓包
    内存泄漏
    字符集编码和排列规则
  • 原文地址:https://www.cnblogs.com/MLGB/p/3197956.html
Copyright © 2011-2022 走看看