Http://www.cnblogs.com/mashang/archive/2009/08/01/1536730.html
Http://www.cnblogs.com/artech/archive/2009/05/22/1486761.html
Http://www.cnblogs.com/zhangchenliang/archive/2012/07/26/2610385.html
Troubleshoot thread synchronization async issues?
1. In what way?
2. Why is lock OK? Principle?
Key points:
1. The disadvantage of lock (this) is that after a thread (for example, T1) locks an object by executing a method of that class using "lock" (for example, Lockme () of this example), the entire object cannot be accessed by another thread (for example, the main thread of this example)- This is because many people use lock (c1)-like code when using the class in other threads, such as the main thread of this example.
2. Locking is not just the code in the lock segment, the lock itself is thread -safe.
3. We should use private objects that do not affect other operations as locker.
4. When using lock, the lock object (locker) must be a reference type, if it is a value type, will cause the object to be boxed into a new reference object each time lock is used (in fact, if you use a value type, the C # compiler (3.5.30729.1) An error will be given at compile time).
Http://www.cnblogs.com/apsnet/archive/2012/07/08/2581475.html
I. Why do you have to Lock,lock?
When we use threads, the most efficient way is, of course, asynchronous, where each thread runs at the same time and does not depend on and waits on each other. However, when different threads need to access a resource, it is necessary to synchronize the mechanism, that is, when reading and writing the same resource, we have to make the resource can only be manipulated by one thread at a time to ensure that each operation is effective and instantaneous, that is, the atomicity of its operation. Lock is the most commonly used synchronization method in C #, in the form of lock (OBJECTA) {codeb}.
Lock (OBJECTA) {Codeb} looks simple and actually has three meanings, which is essential for proper use:
1. Has objecta been lock? No, I'll lock it, or I'll wait until Objecta is released.
2. After lock is executed codeb other threads cannot call Codeb and cannot use Objecta.
3. Objecta is released after execution of Codeb, and codeb can be accessed by other threads.
classprogram{Static voidMain (string[] args) {Console.WriteLine ("The main thread test begins :"); Asyncmethod (); Thread.Sleep ( +); Console.WriteLine ("end of Main thread test :"); Console.ReadLine (); } Static Async voidAsyncmethod () {Console.WriteLine ("Start Async Code"); varresult =awaitMyMethod (); Console.WriteLine ("Asynchronous code Execution complete"); } Static Asynctask<int>MyMethod () { for(inti =0; I <5; i++) {Console.WriteLine ("Asynchronous Execution"+ i.tostring () +".."); awaitTask.delay ( +);//simulate time-consuming operations } return 0; }}
It is obvious that we have written the asynchronous method as well as the synchronous method, and the code is clearer.
You can use the AWAIT keyword internally only if you have async. an async method can have a return type of Task, task<>, or void;
The await keyword is a method for returning a value that is a "waiting" type (awaitable)
Synchronous asynchronous Problem Classic article!!!