C # What is the difference between lock (obj) and lock (this )?

Source: Internet
Author: User

 

 

Lock (obj) lock the obj object

Lock (this) locks the current instance object. If there are multiple class instances, the lock only locks the current class instance and has no effect on other class instances.

Directly add the code.

The main form code is as follows:

Delegate void SetTextCallback (string text );

Public Form1 ()

{

InitializeComponent ();

}

/// <Summary>

/// Use delegate to set text box content

/// </Summary>

/// <Param name = "text"> </param>

Public void SetText (string text)

{

If (this. textBox1.InvokeRequired)

{

SetTextCallback d = new SetTextCallback (SetText );

This. Invoke (d, new object [] {text });

}

Else

{

This. textBox1.Text = this. textBox1.Text + "\ r \ n" + text;

}

}

 

Private void button#click (object sender, EventArgs e)

{

TextBox1.Text = "";

Thread [] thd = new Thread [500];

Int intI = 0;

For (intI = 0; intI <50; intI ++)

{

Thd [intI] = new Thread (new ParameterizedThreadStart (thdText ));

Thd [intI]. Name = "Thread" + intI. ToString ();

Thd [intI]. IsBackground = true;

Thd [intI]. Start (intI );

}

}

/// <Summary>

/// Method called by the thread

/// </Summary>

/// <Param name = "obj"> </param>

Private void thdText (object obj)

{

Merge op = new Merge ();

Int intI = Convert. ToInt32 (obj );

SetText (op. addition ());

}

1. lock (obj)

Add the category class. The Code is as follows:

Public class Program

{

Private static object obj = new object ();

 

Private static Single slgTotal;

 

Public string addition ()

{

Lock (obj)

{

Int intI = 0;

SlgTotal = 0;

For (intI = 0; intI <= 50; intI ++)

{

SlgTotal = slgTotal + intI;

Thread. Sleep (5 );

}

 

Return slgTotal. ToString () + "thread:" + Thread. CurrentThread. Name;

}

}

}

The execution result is as follows:

We can see that the execution results of each thread are the same. Let's take a look at lock (this)

 

2. lock (this)

Modify the lifecycle code to the following:

Public class Program

{

Private static object obj = new object ();

 

Private static Single slgTotal;

 

Public string addition ()

{

Lock (this)

{

Int intI = 0;

SlgTotal = 0;

For (intI = 0; intI <= 50; intI ++)

{

SlgTotal = slgTotal + intI;

Thread. Sleep (5 );

}

 

Return slgTotal. ToString () + "thread:" + Thread. CurrentThread. Name;

}

}

}

The execution result is as follows:

 

We can see that the execution results of each thread are different.

Analysis: the lock (this) lock Object is the current class instance, and each thread operates on a new worker instance. lock (this) only applies to the current instance, slgTotal is a static variable of the class, and lock (this) is actually not starting the desired result. Next, let's look at a lock (obj) instance.

3. the demo of the first obj of lock (obj) is slightly different. That is, the obj static variable of the values class is changed to a variable, and the values class is changed to the following:

Public class Program

{

Private object obj = new object ();

 

Private static Single slgTotal;

 

Public string addition ()

{

Lock (obj)

{

Int intI = 0;

SlgTotal = 0;

For (intI = 0; intI <= 50; intI ++)

{

SlgTotal = slgTotal + intI;

Thread. Sleep (5 );

}

 

Return slgTotal. ToString () + "thread:" + Thread. CurrentThread. Name;

}

}

}

The execution result is as follows:

The running result is the same as the lock (this) result. Why?

 

Summary: In fact, you should not check whether the lock is this or obj. You only need to check whether the multi-thread lock object is the same object. If it is the same object, we will get the result of demo1 above. Otherwise, the result in demo2 and demo3 is not what we want.

From: Li Guoqing

Related Article

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.