. Net Lock (this), Lock (obj), Lock (& amp; quot; string & amp; quot;) console app demo, thread security

Source: Internet
Author: User

. Net Lock (this), Lock (obj), Lock (& quot; string & quot;) console app demo, thread security
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 conclusion !! Lock (this) is only valid for the current instance. lock (obj) is secure and lock ("string") is meaningless.




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 );
}
}
}

}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.