理解c#多线程中的lock关键字
来源:互联网 发布:手机家庭监控器软件 编辑:程序博客网 时间:2024/09/21 11:23
using System;
using System.Threading;
internal class Account
{
int balance;
Random r = new Random();
internal Account(int initial)
{
balance = initial;
}
internal int Withdraw(int amount)
{
if (balance < 0)
{
throw new Exception("Negative Balance");
}
lock (this)
{
Console.WriteLine("Current Thread:" + Thread.CurrentThread.Name);
if (balance >= amount)
{
Thread.Sleep(50);
balance = balance - amount;
return amount;
}
else
{
return 0;
}
}
}
internal void DoTransactions()
{
for (int i = 0; i < 100; i++)
Withdraw(r.Next(-50, 100));
}
}
internal class Test
{
static internal Thread[] threads = new Thread[10];
public static void Main()
{
Account acc = new Account(0);
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].Name = i.ToString();
for (int i = 0; i < 10; i++)
threads[i].Start();
Console.ReadLine();
}
}
- 理解c#多线程中的lock关键字
- C#中的lock关键字
- C#中的lock关键字
- C#中的lock关键字
- C#中的lock关键字
- C#中的lock关键字
- C#中的lock关键字
- 描述C#多线程中 lock关键字
- 描述C#多线程中 lock关键字
- c#多线程中Lock()关键字的使用
- 描述C#多线程中 lock关键字
- 深入理解c#中的lock
- C#中的lock关键字有何用!
- C#中的lock关键字(初识)
- Java 多线程:Lock 关键字
- Java 多线程:Lock 关键字
- 理解C#中的ref关键字
- c#多线程中Lock()关键字的用法小结
- lexicographical_compare()按字典序比较函数用法示例(字符串排序)
- 微软一站式示例代码库 2010年11月更新
- 将ClistCtr中数据导出到Excel表单中
- python中super的一些解释
- QT:QTScript绑定C++的几种方法
- 理解c#多线程中的lock关键字
- asp.net2.0 c#格式化数据 日期格式设置示例 format使用 DataFormatString使用
- C#后台获得前台JS数据值
- ORA
- Wrong permissions on configuration file, should not be world writable!
- 改变Android 对话框位置及边框
- CListCtrl打印、导出到Excel
- [世界上最蛋疼的设计]之(一)“炫彩指示灯”
- 安装Delphi 2010.3615