代码改变世界

C#多线程Lock锁定的使用例子(多线程线程同步)

2011-12-02 01:13  Andrew.Wangxu  阅读(543)  评论(3编辑  收藏  举报

这个例子是一个模拟多个人在多台提款机上同时提取一个账户的款的情况。

在存取的过程中 可能 A线程取了100   而B线程那边还看见账户上没少掉那100快,所以导致数据不统一,赋值出现问题。

下面代码则可以测试出加上Lock锁定 与 不加的区别。

 

先上两个图。

第一个(加了Lock的程序):

 

加了的 数据每一条都是完整无错的,因为他们执行Lock包含的代码时,是一个线程一个线程的执行,而不是一拥而上 都在混乱的执行,那变量就会造成不稳定的情况。

 

下面是不加Lock的代码。

仔细看选中行 与 下一行的余额数目。。

 

按理说下一行 余额应该是509的,可是 却条到了下下行。。10个线程都在取钱 对变量进行操作。。所以会出现这个问题。

 

这就是Lock的好处。

下面贴代码

 

Form1窗体代码

引入命名控件:

using System.Threading; 

 

完整代码:

namespace ThreadExample  
{
public partial class Form1 : Form
{
Thread thread1, thread2;
Class1 class1;
public Form1()
{
InitializeComponent();
class1 = new Class1(this);
}

private void btn_Start_Click(object sender, EventArgs e)
{
txt_Text.Text = "";
class1.shouldStop = false;
thread1 = new Thread(class1.Method1);
thread1.IsBackground = true;
thread2 = new Thread(class1.Method2);
thread2.IsBackground = true;
thread1.Start("a method start\n");
thread2.Start();
}

private delegate void AddMessageDelegate(string message);
public void AddMessage(string message)
{
if (txt_Text.InvokeRequired)
{
AddMessageDelegate d = AddMessage;
txt_Text.Invoke(d, message);
}
else
txt_Text.AppendText(message);
}

private void btn_Stop_Click(object sender, EventArgs e)
{
class1.shouldStop = true;
thread1.Join(0);
thread2.Join(0);
}
}
}

 

 

Account.cs代码:

引入命名空间:

using System.Threading;  

 

完整代码:

namespace LockExample  
{
class Account
{
private object lockObj = new object();
int balance; //余额
Random r = new Random();
Form1 form1;
public Account(int initial, Form1 form1)
{
this.form1 = form1;
this.balance = initial;
}

/// <summary>
/// 取款
/// </summary>
/// <param name="amount">取款金额</param>
/// <returns>余额</returns>
private int Withdraw(int amount)
{
if (balance < 0)
form1.AddListBoxItem("余额:" + balance + " 已经为负值了,还想取呵!");
//lock (lockObj)
{
if (balance >= amount)
{
string str = Thread.CurrentThread.Name + "取款---";
str += string.Format("取款前余额:{0,-6}取款:{1,-6}", balance, amount);
balance = balance - amount;
str += "取款后余额:" + balance;
form1.AddListBoxItem(str);
return amount;
}
else
return 0;
}
}

/// <summary>
/// 自动取款
/// </summary>
public void DoTransactions()
{
for (int i = 0; i < 100; i++)
{
Withdraw(r.Next(1,100));
}
}
}
}



参考:http://www.wxzzz.com/?id=17