SoFunction
Updated on 2025-03-07

C# Method to control thread queues through Semaphore class

This article describes the method of C# controlling thread queues through the Semaphore class. Share it for your reference. The specific implementation method is as follows:

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
namespace ConsoleApp
{
 /// <summary>
 /// Thread control queue /// Semaphore class /// </summary>
 class Program
 {
  static Semaphore semaphore;
  static void Main(string[] args)
  {
   semaphore = new Semaphore(0, 2);
   Thread thread;
   for (int i = 0; i <= 5; i++)
   {
    thread = new Thread(new ParameterizedThreadStart(Run));
    ("thread_"+());
   }
   (2);
   ();
  }
  static void Run(object obj)
  {
   ();
   ("thread " + () + " into the method");
   (5000);   
   ("_thread " + () + " leave the method");
   ();
  }
 }
}

I hope this article will be helpful to everyone's C# programming.