• lock Thread的作用


    lock在图形的绘制方面有一定的应用,
    特别是当要求结束一个计算后,再进行绘图时,常会将计算的任务用lock, 最后进行绘图

           Thread myThread;
            Thread myThread1;

            public void Add()
            {
                  {
                    for (int i = 100; i >= 0; i--)
                    {
                        Thread.Sleep(200);
                        label1.Text = i.ToString();
                    }
                }
            }

            public void Add1()
            {
                {
                    for (int j = 0; j <= 30; j++)
                    {
                        Thread.Sleep(200);
                        label2.Text = j.ToString();
                    }
                }
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                myThread = new Thread(new ThreadStart(Add));
                myThread1 = new Thread(new ThreadStart(Add1));
                myThread.Start();
                myThread1.Start();
            }

    如果没有lock其中的Add 或add1,那么,将动态显示两组数值的变化,
    当加上lock后,就会等其中的add或aa1运行结束后,再时行(绘图其它操作)

         Thread myThread;
            Thread myThread1;

            public void Add()
            {
                //lock (this)
                {
                    for (int i = 100; i >= 0; i--)
                    {
                        Thread.Sleep(200);
                        label1.Text = i.ToString();
                    }
                }
            }

            public void Add1()
            {
                lock (this)   //Add1结束后,显示Form1,
                {
                    for (int j = 0; j <= 30; j++)
                    {
                        Thread.Sleep(200);
                        label2.Text = j.ToString();
                    }
                }
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                myThread = new Thread(new ThreadStart(Add));
                myThread1 = new Thread(new ThreadStart(Add1));
                myThread.Start();
                myThread1.Start();
            }

  • 相关阅读:
    json转化成实体
    mysql基础知识点三排序分组分页优化
    leetcode4majorityElement
    mysql基础知识点四事务隔离与锁机制
    相应内容格式 gzip
    特殊时间格式转化成date
    20220221_摘抄_一别两宽,各生欢喜
    leetcode数学完美数
    leetocde前缀和
    第五轮算法刷题顺序
  • 原文地址:https://www.cnblogs.com/greencolor/p/1578188.html
Copyright © 2020-2023  润新知