一个线程中lock用法的经典实例
/*
该实例是一个线程中lock用法的经典实例,使得到的balance不会为负数
同时初始化十个线程,启动十个,但由于加锁,能够启动调用WithDraw方法的可能只能是其中几个
作者:http://hi.baidu.com/jiang_yy_jiang
*/
using System; namespace ThreadTest29
{
class Account
{
private Object thisLock = new object();
int balance;
Random r = new Random(); public Account(int initial)
{
balance = initial;
} int WithDraw(int amount)
{
if (balance < )
{
throw new Exception("负的Balance.");
}
//确保只有一个线程使用资源,一个进入临界状态,使用对象互斥锁,10个启动了的线程不能全部执行该方法
lock (thisLock)
{
if (balance >= amount)
{
Console.WriteLine("----------------------------:" + System.Threading.Thread.CurrentThread.Name + "---------------"); Console.WriteLine("调用Withdrawal之前的Balance:" + balance);
Console.WriteLine("把Amount输入 Withdrawal :-" + amount);
//如果没有加对象互斥锁,则可能10个线程都执行下面的减法,加减法所耗时间片段非常小,可能多个线程同时执行,出现负数。
balance = balance - amount;
Console.WriteLine("调用Withdrawal之后的Balance :" + balance);
return amount;
}
else
{
//最终结果
return ;
}
}
}
public void DoTransactions()
{
for (int i = ; i < ; i++)
{
//生成balance的被减数amount的随机数
WithDraw(r.Next(, ));
}
}
} class Test
{
static void Main(string[] args)
{
//初始化10个线程
System.Threading.Thread[] threads = new System.Threading.Thread[];
//把balance初始化设定为1000
Account acc = new Account();
for (int i = ; i < ; i++)
{
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(acc.DoTransactions));
threads[i] = t;
threads[i].Name = "Thread" + i.ToString();
}
for (int i = ; i < ; i++)
{
threads[i].Start();
}
Console.ReadKey();
}
}
}
一个线程中lock用法的经典实例的更多相关文章
- c#初学-多线程中lock用法的经典实例
本文转载自:http://www.cnblogs.com/promise-7/articles/2354077.html 一.Lock定义 lock 关键字可以用来确保代码块完成运行,而不会被 ...
- 多线程中lock用法的经典实例
多线程中lock用法的经典实例 一.Lock定义 lock 关键字可以用来确保代码块完成运行,而不会被其他线程中断.它可以把一段代码定义为互斥段(critical section),互斥段在一 ...
- spring事务 将多个connection放到一个线程中
spring事务 将多个connection放到一个线程中
- (未使用AOP)使用ThreadLocal对象把Connection和当前线程绑定, 从而使一个线程中只有一个能控制事务的对象
每个连接都有自己的独立事务,会造成数据的不一致 这组操作应该要么一起操作成功,要么一起操作失败, 应该使用同一个连接,只有一个能控制事务的对象 需要使用ThreadLocal对象把Connection ...
- 在另一个线程中无法用((CMainFrame *)AfxGetMainWnd())
一个vc6的项目放到vc8下重新编译这里死活过不去 查了些资料无果后来翻到一句老外的回答 If AfxGetMainWnd is called from the application’s prima ...
- C#线程中LOCK的意义
学习心得,为的是让新人能理解,高手直接绕~ lock 确保当一个线程位于代码的临界区时,另一个线程不进入临界区.如果其他线程试图进入锁定的代码,则它将一直等待(即被阻止),直到该对象被释放. 引用一句 ...
- WinForm,在另一个线程中更新Form中的数据(转)
Form本身有线程,但对于一些耗时的操作,我们不希望在Form的线程中进行,因为会导致Form线程阻塞,产生假死的现象. 其他线程中操作Form中的控件,总出现“线程间操作无效: 从不是创建控件的线程 ...
- 一个Activity中使用两个layout实例
package com.sbs.aas2l; import android.app.Activity; import android.os.Bundle; import android.view.Vi ...
- C#中Lock静态字段和实例字段
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
随机推荐
- python入门之正则表达式
正则 通过re模块实现 eg:>>>import re >>>re.findall('abc',str_name) 在strname里面完全匹配字符串 ...
- 2个rman自动恢复的脚本
### scripts 1--the scirpt is used for restore db from vcs to a point to time recovery--and the targe ...
- sftp 常用命令 以及 以及与 scp 的比较
1.scp 不能容忍网络闪断,因此一旦出现网络闪断,那么scp 命令就会异常退出 sftp 可以容忍网络闪断,而且具备断电续传,因此sftp 适用于网络更慢的环境, 2. sftp 是一个交互式文件传 ...
- 535 Encode and Decode TinyURL 编码和解码精简URL地址
详见:https://leetcode.com/problems/encode-and-decode-tinyurl/description/ C++: class Solution { public ...
- Y2分班考试 笔试题总结
1. 此题编译错误 base无法点出methodB()方法 2. 第二题选C 3.此题选D:正确的输出级别为fatal>error>warn>info>debug 4. 此题 ...
- Jsoup获取全国地区数据(省市县镇村)(续) 纯干货分享
前几天给大家分享了一下,怎么样通过jsoup来从国家统计局官网获取全国省市县镇村的数据.错过的朋友请点击这里.上文说到抓取到数据以后,我们怎么转换成我们想要格式呢?哈哈,解析方式可能很简单,但是有一点 ...
- Microsoft Sql server2005的安装步骤和常见问题解决方案
一:安装sql server 2005过程中出现 如下问题:“选择的功能中没有任何功能可以安装或升级”: 解决方案:Microsoft SQL Server 2005→配置工具→SQL配置管理器→SQ ...
- iOS perform action after period of inactivity (no user interaction)
代码看完后感觉非常优秀 http://stackoverflow.com/questions/8085188/ios-perform-action-after-period-of-inactivity ...
- NTFS文件系统结构及文件恢复
结构部分参考了 https://www.cnblogs.com/mwwf-blogs/archive/2015/05/04/4467687.html 以及P老师的课件. 文件恢复参考: https: ...
- Maven01
1. Maven简单介绍 Apache Maven是个项目管理和自动构建工具,基于项目对象模型(POM)的概念. 作用:完成项目的相关操作,如:编译,构建,单元测试,安装,网站生成和基于Maven部 ...