Perform
The boss told me to do a little test.
The specific requirements are at a certain point in time, only n threads in the concurrent execution, if there are redundant threads, then wait in line ~
It was really a painstaking effort ah ~ eventually I conquered the ~
Here's what I'm going to say about concrete implementations.
C # provides both thread-related classes of mutexes and interlocked, all under the threading namespace ~!
Witeone,releasemutex Two instance methods are provided in the mutex ~
The Witeone function is to "block the current thread and provide atomic operations on that thread"
That is to say, when a thread encounters a witeone, if there is no thread in Witeone to operate, then the line Cheng to operate
And when the thread is inside, all threads to this need to queue up and wait for the inside thread to execute.
And the end tag that controls such an operation is using the ReleaseMutex Method!
It's like Witeone is a lock, and ReleaseMutex is a key.
When 10 of people saw the door, the first person to arrive at the door would see no one in the room, and he would go in and lock the door.
The person in the back is naturally waiting at the door, and when he finishes his task in the House, he will open the door with the key!
Go out and give the lock to the second comrade in line, and the second comrade will do the same.
If the first comrade does not give the right of use to the second person after he finishes his task, he quits
Then the room is naturally empty, and the door is locked ~ do not worry ~ the door will automatically open, as long as the previous person is not in the room can ~
And then to say the interlocked, the official note is "to increment or decrement the atomic operation of a variable and then save"
The concept of atomic operation is that there is and only one thread in the operation of this variable ~ No other thread to interfere with the operation
When an atom operation is performed on a variable, the variable is locked and the other thread is inaccessible and can only suspend waiting for this variable to unlock
I feel like I'm actually using a mutex to implement it.
Okay, let's start with a concrete implementation.
public class Mutextest
{
private static int poolflag = 0;//Tag
Private Const int amountthread = 10;//Total Threads
Private Const INT Maxthread = 3//maximum number of executable threads
private static Mutex Muxconsole = new Mutex ();
public static void Main ()
{
for (int i = 0; i < amountthread i + +)
{
Create a specified number of threads
is the thread calling the Run method
Start thread
Thread TRD = new Thread (new ThreadStart (Run));
Trd. Name = "thread" + i;
Trd. Start ();
}
}
public static void Run ()
{
Muxconsole.waitone (); blocking queues
Interlocked.Increment (ref Poolflag); Mark +1
if (Poolflag!= maxthread)//judgment is equal to the upper limit
Muxconsole.releasemutex (); If this thread does not reach the upper limit of the executable thread, continue to open and let the following thread in
Console.WriteLine ("{0} is running ... \ n", Thread.CurrentThread.Name);
Thread.Sleep (5000); Simulate execution
Console.WriteLine ("{0} has been aborted ... \ n", Thread.CurrentThread.Name);
Tags-1
Interlocked.decrement (ref Poolflag);
}
}
The annotation is very full, everybody looks slowly ~ i prepare to use this to WebService load balance above ~
So I can control the number of requests myself ~