Thread Synchronization job (1): Lock, monitor

Source: Internet
Author: User

The developed system has the function of Generating document numbers. My practice is to view the current value of the document number field in the data table, add 1 to generate a new document number and store it. I didn't think too much, but after reading the document, our boss told me that if two users access this document number at the same time, the newly generated number is duplicated in the data table. What should I do?

This involves accessing shared resources by multiple users. If the resource is read-only, there is no problem, but if every user can update it, there will be a data synchronization problem. Speaking of this, I think of the bank withdrawal problem that I learned from the database when I went to school. I remember that the solution was locking. Of course, today we have to solve this problem.

If you investigate the cause, you can know that this actually involves a thread synchronization problem (multiple users actually access the server through multiple threads ), the thread synchronization description and solution are described in detail in msdn.The code about shared access resources (data) in the system is the key code. To implement synchronization, you must lock this code when accessing a thread. After this thread completes access, release the lock and allow access from the second thread, and so on..

There are many solutions. Today, we will explain the frequently used keywords: Monitor and lock.

Test code:

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Threading;

Namespace demo1
{
Class safethread
{
Private Static object OBJ = new object (); // note: the object set here, preferably private or protected, cannot be public. Otherwise, problems may occur.

Static void main ()
{
Thread thread = new thread (New threadstart (FOO ));
Thread. Name = "My thread ";
Thread. Start ();

Thread thread2 = new thread (New threadstart (FOO ));
Thread2.name = "My thread 2 ";
Thread2.start ();

Thread thread3 = new thread (New threadstart (FOO ));
Thread3.name = "My thread 3 ";
Thread3.start ();
}

Private Static void Foo ()
{

Monitor. Enter (OBJ );
{
Console. writeline (thread. currentthread. Name );

Console. writeline ("thead executinged ");
}

Monitor. Exit (OBJ); // comment out this sentence during the test to see the lock effect.
}
}
}

Here, we need to lock part of the Moniter part during timely running. The Code implements access and update to data (resources. More convenient use of lock keywords Private Static void Foo ()
{
Lock (OBJ );
{
// Key code area
Console. writeline (thread. currentthread. Name );

Console. writeline ("thead executinged ");
}
}

In fact, the above two methods are equivalent (msdn ).

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.