C # multithreaded Lock instance

Source: Internet
Author: User

Class Program
{
static void Main (string[] args)
{

Call Lockme in the T1 thread and set deadlock to True (deadlock will occur)
int i = 10;
while (i > 0)
{

thread T1 = new Thread (lockme);
T1. Start (TRUE);
Thread.Sleep (1000);

Console.WriteLine (String. Format ("1.i:{0}, start calling Lockme: {1}", I, System.DateTime.Now));
i = i-1;
}

Console.ReadLine ();
}
public static Object locker = new Object ();
public static void Lockme (Object o)
{

Lock (Locker)
{
Console.WriteLine (String. Format ("2. I am locked: {0} ", System.DateTime.Now));
Thread.Sleep (2000);

}
}
}

Be sure to put the lock object public static Object locker = new Object (); Written on the outside of the method,

It doesn't work if you follow the instructions below
public static void Lockme (Object o)
{
Object locker = new Object ();
Lock (Locker)
{
Console.WriteLine (String. Format ("2. I am locked: {0} ", System.DateTime.Now));
Thread.Sleep (2000);

}
}

============================================

Class Program
{
static void Main (string[] args)
{
C1 C1 = new C1 ();
Call Lockme in the T1 thread and set deadlock to True (deadlock will occur)
int i = 10;
while (i > 0)
{
thread T1 = new Thread (c1. Lockme);

T1. Start (TRUE);
Thread.Sleep (1000);

Console.WriteLine (String. Format ("1.i:{0}, start calling Lockme: {1}", I, System.DateTime.Now));
i = i-1;
}
In the main thread, lock C1
Lock (C1)
//{
Call a method that is not lock
C1. Donotlockme ();
Call the Lock method and attempt to dismiss the deadlock
C1. Lockme (TRUE);
//}
Console.ReadLine ();
}

}
}

Class C1
{
private bool deadlocked = true;
Private Object locker = new Object ();
This method uses lock, and we want lock's code to be accessible only by one thread at a time
public void Lockme (object o)
//{
Lock (This)
// {
while (deadlocked)
// {
deadlocked = (bool) o;
Console.WriteLine ("Foo: I am locked:(");
Thread.Sleep (500);
// }
// }
//}
private bool deadlocked = true;
Private Object locker = new Object ();
This method uses lock, and we want lock's code to be accessible only by one thread at a time
public void Lockme (object o)
{
Lock (Locker)
{
while (deadlocked)
//{
deadlocked = (bool) o;
Console.WriteLine (String. Format ("2. I am locked: {0} ", System.DateTime.Now));
Thread.Sleep (2000);
//}
}
}
Methods that all threads can access concurrently
public void Donotlockme ()
{
Console.WriteLine ("I am not locked:)");
}
}

C # multithreaded Lock instance

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.