In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use multi-threaded lock, I believe that most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Often encounter the need to operate on a certain data at the same time, or read and write a file, for these operations we often can not deal with very well before, since the introduction of the keyword lock in the C # language, the above problems are relatively easy to solve, the following is a simple code.
Public class AccessControl () {private static object privateObjectLock = new object (); public static AccessResult () {lock (privateObjectLock) {/ / data manipulation statement www.elvin.com}
The usage of lock in C # Multithreading
When working as a mailbox receiving gateway, the following requirements are encountered, requiring that a receiving thread be opened for each mailbox, receive from the POP3 server, and then store the mail on a unified FTP server, requiring the mail to be numbered from 1 to receive smoothly.
The method I implemented is to new an instance for each mailbox, and then assign the POP3 email address, user name, password and other parameters respectively. A number synchronization problem is involved here, because each thread that receives the mail executes on its own, so the action of getting the number and incrementing is mutually exclusive.
Represented by a static variable, the number is as follows:
Lass EmailInfo {public static int CurrentNumber;}
Then the step to get this in the current thread is:
_ CurrentNumber=++EmailInfo.CurrentNumber
Although this is a sentence, it is divided into several steps when the computer is running, as follows:
EmialInfo.CurrentNumber plus 1--EmailInfo.CurrentNumber returns the value to _ CurrentNumber. Maybe thread 1 has performed the operation of EmailInfo.CurrentNumber plus 1, but has not yet obtained the return value. Thread 2 then performs the operation of EmailInfo.CurrentNumber plus 1, and then thread 1, and thread 2 gets the same return value, so it loses the function of incrementing sequentially.
At this time to find the method of thread synchronization on the Internet, in fact, use the lock statement to lock the incremental paragraph. The relevant usages introduced are:
Lock (this) {_ CurrentNumber=++EmailInfo.CurrentNumber;}
Thought that this can be successful, who knows or invalid, repeated search found that did not understand the use of lock. Because the material on the Internet, the example is relatively simple, is to directly new an object, and then create multiple threads to run for a function of the object, so it can synchronize as long as lock resides in this. Because there is only one instance in operation at this time, and I am new, I have multiple objects, and lock lives in each of its own instances, so of course it is invalid.
So naturally came up with a solution, lock to live in the same instance on the line. Because the parameters of each mail receiving thread are different, new still gives a few real images, but the method of lock is changed to the following:
Add a static variable to EmailInfo first
Class EmailInfo {public static object syncRoot = new object (); public static int CurrentNumber;}
Then lock is changed to:
Lock (EmailInfo.syncRoot) {_ CurrentNumber=++EmailInfo.CurrentNumber;}
You can achieve the desired effect.
These are all the contents of the article "how to use lock in multithreading in C #". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.