In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use lock in C# multithreading, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let's take a look at it.
Here is a simple piece of code.
Public class AccessControl () {private static object privateObjectLock = new object (); public static AccessResult () {lock (privateObjectLock) {/ / data manipulation statement}
Learning C# Multithreading: mailbox problem
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
Class EmailInfo {public static int CurrentNumber;}
Then the step taken by the current thread is
_ CurrentNumber=++EmailInfo.CurrentNumber
Although this is a sentence, it is divided into multiple 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. And the relevant usage introduced is
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, I still have several real images from new, 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.
Thank you for reading this article carefully. I hope the article "how to use lock in C# multithreading" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.