.net Lock(this),Lock(obj),Lock("string") console app demo,线程安全
2014-09-10 12:11
267 查看
class Program
{
static object obj = new object();
static int balance = 500;
static void Main(string[] args)
{
//todo with lock,Safe thread
//Thread t1 = new Thread(new ThreadStart(Credit));
//t1.Start();
//Thread t2 = new Thread(() => Debit());
//t2.Start();
//todo without lock,thread not safty
//Thread t1 = new Thread(new ThreadStart(CreditNoLock));
//t1.Start();
//Thread t2 = new Thread(() => DebitNoLock());
//t2.Start();
//todo new instance,lock (this---context),Lock Failed
//var account = new Account();
//Thread t1 = new Thread(new ThreadStart(account.Credit));
//t1.Start();
//var account2 = new Account();
//Thread t2 = new Thread(() => account2.Debit());
//t2.Start();
//todo new instance,lock (obj-syncRoot),Lock Successed
var account = new Account();
Thread t1 = new Thread(new ThreadStart(account.CreditLockObj));
t1.Start();
var account2 = new Account();
Thread t2 = new Thread(() => account2.DebitLockObj());
t2.Start();
//todo 结论!! lock(this)只对当前instance有效,lock(obj)安全,lock("string字符串")无意义
Console.ReadKey();
}
static void Credit()
{
lock (obj)
{
for (int i = 0; i < 15; i++)
{
Thread.Sleep(500);
balance += 100;
Console.WriteLine("After crediting,balance is {0}", balance);
}
}
}
private static void Debit()
{
lock (obj)
{
for (int i = 0; i < 15; i++)
{
Thread.Sleep(500);
balance -= 100;
Console.WriteLine("After debiting,balance is {0}", balance);
}
}
}
static void CreditNoLock()
{
for (int i = 0; i < 15; i++)
{
Thread.Sleep(1000);
balance += 100;
Console.WriteLine("After crediting,balance is {0}", balance);
}
}
private static void DebitNoLock()
{
for (int i = 0; i < 15; i++)
{
Thread.Sleep(1000);
balance -= 100;
Console.WriteLine("After debiting,balance is {0}", balance);
}
}
}
public class Account
{
static int balance = 500;
static object obj = new object();
public void Credit()
{
lock (this)
{
for (int i = 0; i < 5; i++)
{
Thread.Sleep(1000);
balance -= 100;
Console.WriteLine("After debiting,balance is {0}", balance);
}
}
}
public void Debit()
{
lock (this)
{
for (int i = 0; i < 5; i++)
{
Thread.Sleep(1000);
balance += 100;
Console.WriteLine("After debiting,balance is {0}", balance);
}
}
}
public void CreditLockObj()
{
lock (obj)
{
for (int i = 0; i < 5; i++)
{
Thread.Sleep(1000);
balance -= 100;
Console.WriteLine("After debiting,balance is {0}", balance);
}
}
}
public void DebitLockObj()
{
lock (obj)
{
for (int i = 0; i < 5; i++)
{
Thread.Sleep(1000);
balance += 100;
Console.WriteLine("After debiting,balance is {0}", balance);
}
}
}
}
{
static object obj = new object();
static int balance = 500;
static void Main(string[] args)
{
//todo with lock,Safe thread
//Thread t1 = new Thread(new ThreadStart(Credit));
//t1.Start();
//Thread t2 = new Thread(() => Debit());
//t2.Start();
//todo without lock,thread not safty
//Thread t1 = new Thread(new ThreadStart(CreditNoLock));
//t1.Start();
//Thread t2 = new Thread(() => DebitNoLock());
//t2.Start();
//todo new instance,lock (this---context),Lock Failed
//var account = new Account();
//Thread t1 = new Thread(new ThreadStart(account.Credit));
//t1.Start();
//var account2 = new Account();
//Thread t2 = new Thread(() => account2.Debit());
//t2.Start();
//todo new instance,lock (obj-syncRoot),Lock Successed
var account = new Account();
Thread t1 = new Thread(new ThreadStart(account.CreditLockObj));
t1.Start();
var account2 = new Account();
Thread t2 = new Thread(() => account2.DebitLockObj());
t2.Start();
//todo 结论!! lock(this)只对当前instance有效,lock(obj)安全,lock("string字符串")无意义
Console.ReadKey();
}
static void Credit()
{
lock (obj)
{
for (int i = 0; i < 15; i++)
{
Thread.Sleep(500);
balance += 100;
Console.WriteLine("After crediting,balance is {0}", balance);
}
}
}
private static void Debit()
{
lock (obj)
{
for (int i = 0; i < 15; i++)
{
Thread.Sleep(500);
balance -= 100;
Console.WriteLine("After debiting,balance is {0}", balance);
}
}
}
static void CreditNoLock()
{
for (int i = 0; i < 15; i++)
{
Thread.Sleep(1000);
balance += 100;
Console.WriteLine("After crediting,balance is {0}", balance);
}
}
private static void DebitNoLock()
{
for (int i = 0; i < 15; i++)
{
Thread.Sleep(1000);
balance -= 100;
Console.WriteLine("After debiting,balance is {0}", balance);
}
}
}
public class Account
{
static int balance = 500;
static object obj = new object();
public void Credit()
{
lock (this)
{
for (int i = 0; i < 5; i++)
{
Thread.Sleep(1000);
balance -= 100;
Console.WriteLine("After debiting,balance is {0}", balance);
}
}
}
public void Debit()
{
lock (this)
{
for (int i = 0; i < 5; i++)
{
Thread.Sleep(1000);
balance += 100;
Console.WriteLine("After debiting,balance is {0}", balance);
}
}
}
public void CreditLockObj()
{
lock (obj)
{
for (int i = 0; i < 5; i++)
{
Thread.Sleep(1000);
balance -= 100;
Console.WriteLine("After debiting,balance is {0}", balance);
}
}
}
public void DebitLockObj()
{
lock (obj)
{
for (int i = 0; i < 5; i++)
{
Thread.Sleep(1000);
balance += 100;
Console.WriteLine("After debiting,balance is {0}", balance);
}
}
}
}
相关文章推荐
- 为什么不要 "lock(this)" ? lock object 并是readonly
- 关于继承parent="Theme.AppCompat"错误:This Activity already has an action bar supplied by the window decor
- Xcode上传App到Appstore,报错Error ITMS-90049:"This bundle is invalid.The bundle...."
- 【iOS开发】-canOpenURL: failed for URL: "xx" - error:"This app is not allowed to query for scheme xx"
- iOS9.0 canOpenURL: failed for URL: "xx" - error:"This app is not allowed to query for scheme xx"
- 1>E1400Dlg.obj : error LNK2019: 无法解析的外部符号 __imp__Demo@12,该符号在函数 "protected: v
- FlashlconDlg.obj : error LNK2001: unresolved external symbol "protected: void __thiscall CFlashlconDlg::OnExit(void)" 的解决
- main.obj:-1: error: LNK2019: 无法解析的外部符号 "public: __thiscall CFindFileForm::CFindFileForm(class QWidge
- 提交app苹果审核时报错ERROR ITMS-90062: "This bundle is invalid. The value for key CFBundleShortVersionString
- VC编译错误:msvcrtd.lib(ti_inst.obj) : error LNK2005: "private: __thiscall type_info::type_info(class typ
- -canOpenURL: failed for URL: "weixin://app/wxff569f1617559XX1/" - error: "This app is not allowed to
- VC编译错误:msvcrtd.lib(ti_inst.obj) : error LNK2005: "private: __thiscall type_info::type_info(class typ
- error LNK2005: "public: __thiscall std::basic_string……already defined in……
- Xcode上传App到Appstore,报错Error ITMS-90049:"This bundle is invalid.The bundle...."
- 关于NSLocalizedString(@"Foo %@",nil) 使用 NSLocalizedString实现App文本的本地化
- Error: An App ID with identifier "*****" is not avaliable. Please enter a different string.
- "This app is not allowed to query for scheme ***"
- c# 多线程中lock用法经典实例及lock("xxx"),lock(typeof(Class)),Lock(this)的区别
- MSVCRT.lib(ti_inst.obj) : error LNK2005: "private: __thiscall type_info::type_info(class type_info const &)" (??0type_info@@AAE@
- Xcode上传App到Appstore时,报错Error ITMS-90049:"This bundle is invalid.The bundle...."