描述C#多線程中l(wèi)ock關(guān)鍵字的使用分析
更新時間:2013年06月05日 15:03:19 作者:
本篇文章是對C#多線程中l(wèi)ock關(guān)鍵字的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
本文介紹C# lock關(guān)鍵字,C#提供了一個關(guān)鍵字lock,它可以把一段代碼定義為互斥段(critical section),互斥段在一個時刻內(nèi)只允許一個線程進(jìn)入執(zhí)行,而其他線程必須等待。
每個線程都有自己的資源,但是代碼區(qū)是共享的,即每個線程都可以執(zhí)行相同的函數(shù)。這可能帶來的問題就是幾個線程同時執(zhí)行一個函數(shù),導(dǎo)致數(shù)據(jù)的混亂,產(chǎn)生不可預(yù)料的結(jié)果,因此我們必須避免這種情況的發(fā)生。
C#提供了一個關(guān)鍵字lock,它可以把一段代碼定義為互斥段(critical section),互斥段在一個時刻內(nèi)只允許一個線程進(jìn)入執(zhí)行,而其他線程必須等待。在C# lock關(guān)鍵字定義如下:
lock(expression) statement_block
expression代表你希望跟蹤的對象,通常是對象引用。
如果你想保護(hù)一個類的實(shí)例,一般地,你可以使用this;如果你想保護(hù)一個靜態(tài)變量(如互斥代碼段在一個靜態(tài)方法內(nèi)部),一般使用類名就可以了。
而statement_block就是互斥段的代碼,這段代碼在一個時刻內(nèi)只可能被一個線程執(zhí)行。
下面是一個使用C# lock關(guān)鍵字的典型例子,在注釋里說明了C# lock關(guān)鍵字的用法和用途。
示例如下:
using System;
using System.Threading;
namespace ThreadSimple
{
internal class Account
{
int balance; //余額
Random r=new Random();
internal Account(int initial)
{
balance=initial;
}
internal int Withdraw(int amount) //取回、取款
{
if(balance<0)
{
//如果balance小于0則拋出異常
throw new Exception("NegativeBalance");//負(fù)的 余額
}
//下面的代碼保證在當(dāng)前線程修改balance的值完成之前
//不會有其他線程也執(zhí)行這段代碼來修改balance的值
//因此,balance的值是不可能小于0的
lock(this)
{
Console.WriteLine("CurrentThread:"+Thread.CurrentThread.Name);
//如果沒有l(wèi)ock關(guān)鍵字的保護(hù),那么可能在執(zhí)行完if的條件判斷(成立)之后
//另外一個線程卻執(zhí)行了balance=balance-amount修改了balance的值
//而這個修改對這個線程是不可見的,所以可能導(dǎo)致這時if的條件已經(jīng)不成立了
//但是,這個線程卻繼續(xù)執(zhí)行 balance=balance-amount,所以導(dǎo)致balance可能小于0
if(balance>=amount)
{
Thread.Sleep(5);
balance=balance-amount;
return amount;
} else
{
return 0;
//transactionrejected
}
}
}
internal void DoTransactions()//取款事務(wù)
{
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();
}
}
}
}
每個線程都有自己的資源,但是代碼區(qū)是共享的,即每個線程都可以執(zhí)行相同的函數(shù)。這可能帶來的問題就是幾個線程同時執(zhí)行一個函數(shù),導(dǎo)致數(shù)據(jù)的混亂,產(chǎn)生不可預(yù)料的結(jié)果,因此我們必須避免這種情況的發(fā)生。
C#提供了一個關(guān)鍵字lock,它可以把一段代碼定義為互斥段(critical section),互斥段在一個時刻內(nèi)只允許一個線程進(jìn)入執(zhí)行,而其他線程必須等待。在C# lock關(guān)鍵字定義如下:
lock(expression) statement_block
expression代表你希望跟蹤的對象,通常是對象引用。
如果你想保護(hù)一個類的實(shí)例,一般地,你可以使用this;如果你想保護(hù)一個靜態(tài)變量(如互斥代碼段在一個靜態(tài)方法內(nèi)部),一般使用類名就可以了。
而statement_block就是互斥段的代碼,這段代碼在一個時刻內(nèi)只可能被一個線程執(zhí)行。
下面是一個使用C# lock關(guān)鍵字的典型例子,在注釋里說明了C# lock關(guān)鍵字的用法和用途。
示例如下:
復(fù)制代碼 代碼如下:
using System;
using System.Threading;
namespace ThreadSimple
{
internal class Account
{
int balance; //余額
Random r=new Random();
internal Account(int initial)
{
balance=initial;
}
internal int Withdraw(int amount) //取回、取款
{
if(balance<0)
{
//如果balance小于0則拋出異常
throw new Exception("NegativeBalance");//負(fù)的 余額
}
//下面的代碼保證在當(dāng)前線程修改balance的值完成之前
//不會有其他線程也執(zhí)行這段代碼來修改balance的值
//因此,balance的值是不可能小于0的
lock(this)
{
Console.WriteLine("CurrentThread:"+Thread.CurrentThread.Name);
//如果沒有l(wèi)ock關(guān)鍵字的保護(hù),那么可能在執(zhí)行完if的條件判斷(成立)之后
//另外一個線程卻執(zhí)行了balance=balance-amount修改了balance的值
//而這個修改對這個線程是不可見的,所以可能導(dǎo)致這時if的條件已經(jīng)不成立了
//但是,這個線程卻繼續(xù)執(zhí)行 balance=balance-amount,所以導(dǎo)致balance可能小于0
if(balance>=amount)
{
Thread.Sleep(5);
balance=balance-amount;
return amount;
} else
{
return 0;
//transactionrejected
}
}
}
internal void DoTransactions()//取款事務(wù)
{
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();
}
}
}
}
相關(guān)文章
C#實(shí)現(xiàn)將網(wǎng)址生成二維碼圖片方法介紹
這篇文章介紹了C#實(shí)現(xiàn)將網(wǎng)址生成二維碼圖片的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04
C#使用FolderBrowserDialog類實(shí)現(xiàn)選擇打開文件夾方法詳解
這篇文章主要介紹了C#選擇文件夾/打開文件夾/瀏覽文件夾等代碼方法,大家參考使用2013-11-11
C#讀寫操作app.config中的數(shù)據(jù)應(yīng)用介紹
C#讀寫操作app.config中的數(shù)據(jù)應(yīng)用介紹;需要的朋友可以參考下2012-11-11

