C # 's Lock keyword

Source: Internet
Author: User

usingSystem;usingSystem.Threading;namespacetest{classProgram {//One, lock definition//The Lock keyword can be used to ensure that a block of code finishes running without being interrupted by another thread. It can define a piece of code as a mutex (critical section),//The mutex only allows one thread to go into execution at a time, while other threads must wait.        This is achieved by obtaining a mutex for a given object during the code block run. //in multi-threading, each thread has its own resources, but the code area is shared, that is, each thread can execute the same function. //The problem is that several threads execute a function at the same time, causing the data to clutter and produce unpredictable results, so we have to avoid this happening. //and in. NET is best to understand the concepts of processes, application domains, and threads, because lock is for thread-level, and in. NET, does the application domain isolate lock//my guess is that threads that are not in the same application domain cannot be interrupted by lock, and it's better to know the concepts of data segments, code snippets, heaps, stacks, and so on. //the C # lock keyword is defined as follows://Lock (expression) Statement_block, where expression represents the object you want to track, typically an object reference. //if you want to protect an instance of a class, generally, you can use this; if you want to protect a static variable (such as a mutex snippet inside a static method),//It is generally possible to use the class name. And Statement_block is the code of the mutex, which can only be executed by one thread at a time.         Static voidMain (string[] args) {Thread Thread1=NewThread (NewThreadStart (ThreadStart1)) {Name ="Thread1" }; Thread thread2=NewThread (NewThreadStart (ThreadStart2)) {Name ="Thread2" }; Thread thread3=NewThread (NewThreadStart (THREADSTART3)) {Name ="Thread3" }; Thread1.            Start (); Thread2.            Start (); Thread3.             Start (); //locking A string is more magical, as long as the string content is the same, it can cause the program to hang. //the reason is that. NET, the string is temporarily stored, if the string contents of the two variables are the same,//. NET assigns a staged string object to the variable.            So if you have two places with lock ("my lock"), they actually lock the same object. //Lock ("string")//Lock (This) if this is a different instance of the public class new, there is no mutex effect//Lock (typeof (MyClass)) locks it and locks all instances of the object        }        Static ReadOnly Object_object =New Object(); Static voidDone (intmillisecondstimeout) {Console.WriteLine (string. Format ("{ 0}, {1}. Start", DateTime.Now.ToString ("HH:mm:ss") , Thread.CurrentThread.Name)); //The code snippet below can only be executed by one thread at a time            Lock(_object) {Console.WriteLine (string. Format ("{0}, {1}, enters the locked area.", DateTime.Now.ToString ("HH:mm:ss") , Thread.CurrentThread.Name));                Thread.Sleep (millisecondstimeout); Console.WriteLine (string. Format ("{0}, {1} exits the locked area.", DateTime.Now.ToString ("HH:mm:ss") , Thread.CurrentThread.Name)); }        }        Static voidThreadStart1 () {Done ( the); }        Static voidThreadStart2 () {Done ( the); }        Static voidThreadStart3 () {Done ( +); }     }}

C # 's Lock keyword

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.