本文介绍C# lockkeyword,C#提供了一个keywordlock,它能够把一段代码定义为相互排斥段(critical section),相互排斥段在一个时刻内仅仅同意一个线程进入运行,而其他线程必须等待。
每个线程都有自己的资源,可是代码区是共享的,即每个线程都能够运行相同的函数。这可能带来的问题就是几个线程同一时候运行一个函数,导致数据的混乱,产生不可预料的结果,因此我们必须避免这种情况的发生。
C#提供了一个keywordlock,它能够把一段代码定义为相互排斥段(critical section),相互排斥段在一个时刻内仅仅同意一个线程进入运行,而其他线程必须等待。在C# lockkeyword定义例如以下:
lock(expression) statement_block
expression代表你希望跟踪的对象,一般是对象引用。
假设你想保护一个类的实例,一般地,你能够使用this;假设你想保护一个静态变量(如相互排斥代码段在一个静态方法内部),一般使用类名就能够了。
而statement_block就是相互排斥段的代码,这段代码在一个时刻内仅仅可能被一个线程运行。
以下是一个使用C# lockkeyword的典型样例,在凝视里说明了C# lockkeyword的使用方法和用途。
演示样比例如以下:
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 " ); // 负的 剩余金额
}
// 以下的代码保证在当前线程改动balance的值完毕之前
// 不会有其他线程也运行这段代码来改动balance的值
// 因此,balance的值是不可能小于0的
lock ( this )
{
Console.WriteLine( " CurrentThread: " + Thread.CurrentThread.Name);
// 假设没有lockkeyword的保护,那么可能在运行完if的条件推断(成立)之后
// 另外一个线程却运行了balance=balance-amount改动了balance的值
// 而这个改动对这个线程是不可见的,所以可能导致这时if的条件已经不成立了
// 可是,这个线程却继续运行 balance=balance-amount,所以导致balance可能小于0
if (balance >= amount)
{
Thread.Sleep( 5 );
balance = balance - amount;
return amount;
} else
{
return 0 ;
// transactionrejected
}
}
}
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();
}
}
}
}
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 " ); // 负的 剩余金额
}
// 以下的代码保证在当前线程改动balance的值完毕之前
// 不会有其他线程也运行这段代码来改动balance的值
// 因此,balance的值是不可能小于0的
lock ( this )
{
Console.WriteLine( " CurrentThread: " + Thread.CurrentThread.Name);
// 假设没有lockkeyword的保护,那么可能在运行完if的条件推断(成立)之后
// 另外一个线程却运行了balance=balance-amount改动了balance的值
// 而这个改动对这个线程是不可见的,所以可能导致这时if的条件已经不成立了
// 可是,这个线程却继续运行 balance=balance-amount,所以导致balance可能小于0
if (balance >= amount)
{
Thread.Sleep( 5 );
balance = balance - amount;
return amount;
} else
{
return 0 ;
// transactionrejected
}
}
}
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();
}
}
}
}