zoukankan      html  css  js  c++  java
  • lock 语句(C# 参考)

    lock 关键字将语句块标记为临界区,方法是获取给定对象的互斥锁,执行语句,然后释放该锁。 此语句的形式如下:

    Object thisLock = new Object();
    lock (thisLock)
    {
        // Critical code section.
    }

    有关更多信息,请参见 线程同步(C# 编程指南)

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

    线程处理(C# 编程指南) 这节讨论了线程处理。

    lock 关键字在块的开始处调用 Enter,而在块的结尾处调用 Exit

    通常,应避免锁定 public 类型,否则实例将超出代码的控制范围。 常见的结构 lock (this)lock (typeof (MyType))lock ("myLock") 违反此准则:

    • 如果实例可以被公共访问,将出现 lock (this) 问题。

    • 如果 MyType 可以被公共访问,将出现 lock (typeof (MyType)) 问题。

    • 由于进程中使用同一字符串的任何其他代码都将共享同一个锁,所以出现 lock(“myLock”) 问题。

    最佳做法是定义 private 对象来锁定, 或 private static 对象变量来保护所有实例所共有的数据。

    下面演示在 C# 中使用未锁定的线程的简单示例。

    //using System.Threading;
    
    class ThreadTest
    {
        public void RunMe()
        {
            Console.WriteLine("RunMe called");
        }
    
        static void Main()
        {
            ThreadTest b = new ThreadTest();
            Thread t = new Thread(b.RunMe);
            t.Start();
        }
    }
    // Output: RunMe called
    
    

    下例使用线程和 lock。 只要 lock 语句存在,语句块就是临界区并且 balance 永远不会是负数。

    // using System.Threading;
    
    class Account
    {
        private Object thisLock = new Object();
        int balance;
    
        Random r = new Random();
    
        public Account(int initial)
        {
            balance = initial;
        }
    
        int Withdraw(int amount)
        {
    
            // This condition will never be true unless the lock statement
            // is commented out:
            if (balance < 0)
            {
                throw new Exception("Negative Balance");
            }
    
            // Comment out the next line to see the effect of leaving out 
            // the lock keyword:
            lock (thisLock)
            {
                if (balance >= amount)
                {
                    Console.WriteLine("Balance before Withdrawal :  " + balance);
                    Console.WriteLine("Amount to Withdraw        : -" + amount);
                    balance = balance - amount;
                    Console.WriteLine("Balance after Withdrawal  :  " + balance);
                    return amount;
                }
                else
                {
                    return 0; // transaction rejected
                }
            }
        }
    
        public void DoTransactions()
        {
            for (int i = 0; i < 100; i++)
            {
                Withdraw(r.Next(1, 100));
            }
        }
    }
    
    class Test
    {
        static void Main()
        {
            Thread[] threads = new Thread[10];
            Account acc = new Account(1000);
            for (int i = 0; i < 10; i++)
            {
                Thread t = new Thread(new ThreadStart(acc.DoTransactions));
                threads[i] = t;
            }
            for (int i = 0; i < 10; i++)
            {
                threads[i].Start();
            }
        }
    }
    
  • 相关阅读:
    Spring Boot中只能有一个WebMvcConfigurationSupport配置类
    【原创】(六)Linux进程调度-实时调度器
    【原创】(二)Linux进程调度器-CPU负载
    【原创】(十二)Linux内存管理之vmap与vmalloc
    【原创】(十一)Linux内存管理slub分配器
    【原创】(七)Linux内存管理
    【原创】(五)Linux内存管理zone_sizes_init
    【原创】(三)Linux paging_init解析
    【原创】(二)Linux物理内存初始化
    【原创】(一)ARMv8 MMU及Linux页表映射
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/1535979.html
Copyright © 2011-2022 走看看