C # multithreading mechanism (4)

Source: Internet
Author: User

C # multithreading mechanism (4)

Expression indicates the object you want to trace. It is usually an object reference. Generally, if you want to protect an instance of a class, you can use this; if you want to protect a static variable (such as a mutex code segment inside a static method ), generally, you can use a class name. The statement_block is the code of the mutex, which can be executed by only one thread at a time.

The following is a typical example of using the lock keyword. I will explain the usage and usage of the lock keyword in the comment:

// Lock. cs
Using System;
Using System. Threading;

Internal class Account
{
Int balance;
Random r = new Random ();
Internal Account (int initial)
{
Balance = initial;
}

Internal int Withdraw (int amount)
{
If (balance <0)
{
// If the balance is smaller than 0, an exception is thrown.
Throw new Exception ("Negative Balance ");
}
// The following Code ensures that the balance value is modified by the current thread.
// No other threads will execute this code to modify the balance value.
// Therefore, the balance value cannot be smaller than 0.
Lock (this)
{
Console. WriteLine ("Current Thread:" + Thread. CurrentThread. Name );
// If the lock keyword is not protected, it may be after the if condition is determined.
// Another thread executes the balance = balance-amount command to modify the balance value.
// This modification is invisible to this thread, so it may cause the if condition to be invalid.
// However, this thread continues to execute balance = balance-amount, so the balance may be less than 0
If (balance> = amount)
{
Thread. Sleep (5 );
Balance = balance-amount;
Return amount;
}
Else
{
Return 0; // transaction rejected
}
}
}
Internal void DoTransactions ()
{
For (int I = 0; I <100; I ++)
Withdraw (r. Next (-50,100 ));
}
}

Internal class Test
{
Static internal Thread [] threads = new Thread [10];
Public static void Main ()
{
Account acc = new Account (0 );
For (int I = 0; I <10; I ++)
{
Thread t = new Thread (new ThreadStart (acc. DoTransactions ));
Threads [I] = t;
}
For (int I = 0; I <10; I ++)
Threads [I]. Name = I. ToString ();
For (int I = 0; I <10; I ++)
Threads [I]. Start ();
Console. ReadLine ();
}
}

When multiple threads share an object, there will also be a problem similar to the Public Code. In this case, you should not use the lock keyword. here you need to use System. A kind of Monitor In Threading is called a Monitor, which provides a solution to share resources with threads.

The Monitor class can lock an object. A thread can operate on this object only when this lock is obtained. The object lock mechanism ensures that only one thread can access this object at a time point that may cause confusion. Monitor must be associated with a specific object, but because it is a static class, it cannot be used to define the object, and all its methods are static, objects cannot be referenced. The following code uses Monitor to lock an object:

......
Queue oQueue = new Queue ();
......
Monitor. Enter (oQueue );
... // Now the oQueue object can only be manipulated by the current thread
Monitor. Exit (oQueue); // release the lock

As shown above, when a thread calls Monitor. when the Enter () method locks an object, this object will be owned by it. Other threads want to access this object, only waiting for it to use Monitor. the Exit () method releases the lock. To ensure that the thread can release the lock in the end, you can write the Monitor. Exit () method into the finally code block in the try-catch-finally structure. For any Monitor-locked object, some information related to it is stored in the memory. One is the reference of the thread currently holding the lock, and the other is a reserve queue column, the queue stores the thread that is ready to obtain the lock. The third is a waiting queue, which stores the reference of the queue that is currently waiting for this object to change its status. When the thread that owns the object lock is about to release the lock, it uses Monitor. the Pulse () method notifies the first thread in the waiting queue, so the thread is transferred to the reserve queue column. When the object lock is released, the thread in the reserve queue column can immediately obtain the object lock.

The following is an example of how to use the lock keyword and the Monitor class to synchronize and communicate threads. It is also a typical producer and consumer problem. In this routine, the producer thread and consumer thread are alternating. When the producer writes a number, the consumer immediately reads and displays it. I will introduce the essence of the program in the annotations. The system namespace used is as follows:

Using System;
Using System. Threading;


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.