In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the method of C# thread pool and file download server". The explanation in this article is simple and clear and easy to learn and understand. let's study and learn "what is the method of C# thread pool and file download server"!
Although threads can improve the efficiency of running programs to some extent, they also have some side effects. Let's first look at the following code:
Class Increment {private int n = 0; private int max; public Increment (int max) {this.max = max;} public int result {get {return n;} set {n = value }} public void Inc () {for (int I = 0; I < max; iTunes +) {class Program {public static void Main () {Increment inc = new Increment (10000) Thread [] threads = new Thread [30]; for (int I = 0; I < threads.Length; iTunes +) {threads [I] = new Thread (inc.Inc); thread [I] .start ();} for (int I = 0; I < threads.Length) Console.WriteLine +) {thread [I] .join (); / wait for 30 threads to finish executing} thread (inc.result); / / output the value of n}}
The basic function of the above program is to increment max n using Increment's Inc method, except that 30 threads will be started in the Main method to execute the Inc method at the same time. In this case, the value of max is 10000 (passed in through the constructor of Increment). Readers can run this program, the normal result should be 300000, but usually will not get this result, generally the result is smaller than 300000. The reason is that nasty + in the Inc method is a simple self-increasing language, but from the bottom analysis, the IL code of nasty + is as follows:
Ldsfld / / gets the initial value of n and presses 1 into the method stack ldc.i4.1 / / pushes 1 into the method stack add / / pops the top two values from the method stack, adds up, then saves the result in the method stack stfld / / pops a value from the current method stack, and updates the class field n
Each of the above IL statements is thread-safe, but the nasty + C # statement requires the above four steps to complete, so the nasty + statement is not thread-safe. As long as any step before the execution of the stfld instruction is interrupted because another thread obtains the CPU, so-called "dirty" data occurs.
Assuming that the initial value of n is 0, thread1 is interrupted by thread2 after executing ldc.i4.1 (the add instruction is not executed), then the initial value of n obtained by thread2 is still 0, assuming that thread2 is successfully executed, then the value of n is already 1. When thread2 is finished, thread1 continues to execute add instructions, and thread1 executes smoothly, at this time, the execution result n in thread1 is still 1. Therefore, this leads to the case that the n is still 1 for two calls to the naughty scene. The way to solve this problem is also easy to think of, that is, let the above four IL statements are either not executed, all will be executed, which is a bit of a transaction.
The technique for solving this problem in C # threads is called synchronization. The essence of synchronization is to lock a block of code to make it a whole and advance and retreat together. The simplest thing is to use lock to lock the code block. This sentence has been used many times in previous lectures. The lock statement can lock any object. If you are locking a class member, you can directly use the form of lock (obj). If you are locking a static member, you can put the lock on the object type as follows:
Lock (typeof (StaticClass)) {...}
For the Increment class, we can lock the Increment class or the Inc method. For example, the code for locking the nymph + class is as follows:
Class Increment {private int n = 0; private int max; private Object lockN = new Object (); public Increment (int max) {this.max = max;} public int result {get {return n;} set {n = value }} private void IncN () {lock (lockN) {nails;}} public void Inc () {for (int I = 0; I < max; iTunes +) {IncN () }}}
You can also put the following code directly into the for loop instead of calling the IncN method
Lock (lockN) {nemesis;}
Or lock the Inc method directly, as follows:
Public void Inc () {lock (lockN) {for (int I = 0; I < max; iTunes +) {for;}
However, the author does not recommend locking the Inc directly, because it is no different from a single thread, although it can avoid reading dirty data, but at the expense of efficiency.
From the analysis of this example, we know that the cause of the problem is that nasty + is not an atomic operation. An Interlocked class is provided in. Net framework to make nasty + an atomic operation. Interlocked has some methods that can guarantee that the operation on variables is atomic, such as Increment method ensures that the operation of nmol + is atomic, Decrement method ensures that the operation of nMurray-is atomic, and Exchange method ensures that the operation of assigning variables is atomic. Therefore, you can use the Increment method to replace the following code:
Public void Inc () {for (int I = 0; I < max; iTunes +) {Interlocked.Increment (ref n);}}
Everything has two sides, and synchronization technology is no exception. In some cases, deadlocks can be caused by two threads locking objects to each other (that is, two threads waiting for each other to release objects). It's like two students reviewing their lessons in the evening. They both hope to overtake each other in their studies, and they are both very tired, but neither of them wants to rest first. They are staring at the lights in each other's room, expecting each other to have a rest. Take a rest on your own. But no one would turn off the lights first, so they had to wait until dawn. Of course, there are two ways to solve this problem. One or two of the students simply don't care whether the other person goes to bed first. When they are tired of learning, they turn off the lights directly. Of course, another method is a bit violent, which is to turn off the power directly at the right time, so no one should learn it (this is also equivalent to thread interruption, but don't use it until it's too late).
Let's start with an example of a thread deadlock, the code is as follows:
Class Program {private static Object objA = new Object (); private static Object objB = new Object (); public static void LockA () {lock (objA) {Thread.Sleep (1000); lock (objB) {}} Console.WriteLine ("LockA") } public static void LockB () {lock (objB) {Thread.Sleep (2000); lock (objA) {}} Console.WriteLine ("LockB");} public static void Main () {Thread threadA = new Thread (LockA) Thread threadB = new Thread (LockB); threadA.Start (); threadB.Start ();}}
In the above code, the LockA method delays 1 second after executing Lock (objA) in the current thread, while the LockB method delays 2 seconds after executing lock (objB). Usually LockA executes lock (objB) first, but the objB is locked by LockB and LockB is still delayed (less than 2 seconds). At this point, LockA has locked both objA and objB, and when LockB executes to lock (objA), LockB is blocked because objA is already locked. When LockA executes to lock (objB), because LockA is still lagging, objB is also locked. LockA and LockB are equivalent to the above two students, waiting for each other to turn off the lights, but neither of them is willing to turn off the lights first, so they are deadlocked. If you use *, it is very simple to keep the order of multiple locked objects. For example, change the locking order of the LockB method. The code is as follows:
Public static void LockB () {lock (objA) {Thread.Sleep (2000); lock (objB) {} Console.WriteLine ("LockB");}
Or change the LockA method to lock objB first and then objA.
Of course, you can also use a more violent approach, and when you find that some threads are unresponsive for a long time, you can use the Abort method to forcibly interrupt them. The code is as follows:
Public static void Main () {Thread threadA = new Thread (LockA); Thread threadB = new Thread (LockB); threadA.Start (); threadB.Start (); Thread.Sleep (4000); threadA.Abort (); threadB.Abort (); Console.WriteLine (Thread ends all) Thank you for your reading, the above is the content of "what is the method of C# thread pool and file download server". After the study of this article, I believe you have a deeper understanding of what the method of C # thread pool and file download server is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.