按你的要求,可以在线程执行的函数中,一开始就开始锁定 在以下例子中,线程函数Withdraw在开始计算前,先将对将自己锁定,防止其他线程对其变量balance修改。using System; using System.Threading;class Account { int balance; Random r = new Random(); public Account(int initial) { balance = initial; } int Withdraw(int amount) { // This condition will never be true unless the lock statement // is commented out: if (balance < 0) { throw new Exception("Negative Balance"); } // Comment out the next line to see the effect of leaving out // the lock keyword: lock (this) { if (balance >= amount) { Console.WriteLine("Balance before Withdrawal : " + balance); Console.WriteLine("Amount to Withdraw : -" + amount); balance = balance - amount; Console.WriteLine("Balance after Withdrawal : " + balance); return amount; } else { return 0; // transaction rejected } } } public void DoTransactions() { for (int i = 0; i < 100; i++) { Withdraw(r.Next(1, 100)); } } }class Test { public static void Main() { Thread[] threads = new Thread[10]; Account acc = new Account (1000); 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].Start(); } } } 另一个例子 class MyClass { public static int ItemCount = 0; }class 线程1 { public void AddItem() { lock(MyClass.ItemCount) { list.Add(...); MyClass.ItemCount++; } } } class 线程2 { public void RemoveItem() { lock(MyClass.ItemCount) { list.Remove(...); MyClass.ItemCount--; } } }
同一线程的代码是顺序执行的,可以避免多次lock
greenery好厉害,我也是来抢分的,嘿嘿
学习…… dot net中好像有相应的锁类
我当前的实现,大家帮我看看有什么问题没? //多线程同步锁 bool _locked; public object SyncRoot { get { return _dd; } } public void Lock(Guid documentGuid) { _dd.Current=documentGuid; System.Threading.Monitor.Enter(SyncRoot); if(_locked) { System.Threading.Monitor.Exit(SyncRoot); } else { _locked=true; } } public void Unlock() { System.Threading.Monitor.Exit(SyncRoot); System.Threading.Monitor.Exit(SyncRoot); System.Threading.Monitor.Exit(SyncRoot); _locked=false; } public bool Locked { get { return _locked; } }
在以下例子中,线程函数Withdraw在开始计算前,先将对将自己锁定,防止其他线程对其变量balance修改。using System;
using System.Threading;class Account
{
int balance; Random r = new Random(); public Account(int initial)
{
balance = initial;
} int Withdraw(int amount)
{ // This condition will never be true unless the lock statement
// is commented out:
if (balance < 0)
{
throw new Exception("Negative Balance");
} // Comment out the next line to see the effect of leaving out
// the lock keyword:
lock (this)
{
if (balance >= amount)
{
Console.WriteLine("Balance before Withdrawal : " + balance);
Console.WriteLine("Amount to Withdraw : -" + amount);
balance = balance - amount;
Console.WriteLine("Balance after Withdrawal : " + balance);
return amount;
}
else
{
return 0; // transaction rejected
}
}
} public void DoTransactions()
{
for (int i = 0; i < 100; i++)
{
Withdraw(r.Next(1, 100));
}
}
}class Test
{
public static void Main()
{
Thread[] threads = new Thread[10];
Account acc = new Account (1000);
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].Start();
}
}
}
另一个例子
class MyClass
{
public static int ItemCount = 0;
}class 线程1
{
public void AddItem()
{
lock(MyClass.ItemCount)
{
list.Add(...);
MyClass.ItemCount++;
}
}
}
class 线程2
{
public void RemoveItem()
{
lock(MyClass.ItemCount)
{
list.Remove(...);
MyClass.ItemCount--;
}
}
}
dot net中好像有相应的锁类
bool _locked;
public object SyncRoot
{
get
{
return _dd;
}
}
public void Lock(Guid documentGuid)
{
_dd.Current=documentGuid; System.Threading.Monitor.Enter(SyncRoot);
if(_locked)
{
System.Threading.Monitor.Exit(SyncRoot);
}
else
{
_locked=true;
}
} public void Unlock()
{
System.Threading.Monitor.Exit(SyncRoot);
System.Threading.Monitor.Exit(SyncRoot);
System.Threading.Monitor.Exit(SyncRoot);
_locked=false;
} public bool Locked
{
get
{
return _locked;
}
}