zoukankan      html  css  js  c++  java
  • 【摘要】Thread and Lock

    The lock keyword marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object, executing a statement, and then releasing the lock. This statement takes the following form:
     
    Object thisLock = new Object();
    lock (thisLock)
    {
        // Critical code section
    }
     
    /*********************/
     
    The following sample shows a simple use of threads in C#.
    // statements_lock.cs

    using System;
    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();
        }
    }
     
    /*********************/
     
    The following sample uses threads and lock. As long as the lock statement is present, the statement block is a critical section and balance will never become a negative number.
    // statements_lock2.cs

    using System;
    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();
            }
        }
    }
  • 相关阅读:
    友链
    利用jenkins插件查看allure报告
    python pyyaml操作yaml配置文件
    数组类型
    接口测试--加密算法
    python赋值,深拷贝和浅拷贝的区别
    RF中在测试用例集上设置标签
    python中json.dump()与json.dumps()的区别
    python 日期与字符串之间的转换
    python operator操作符函数
  • 原文地址:https://www.cnblogs.com/ShineTan/p/2801616.html
Copyright © 2011-2022 走看看