C#多线程之线程池ThreadPool用法

  using System;

  using System.Threading;

  namespace ThreadPoolDemo

  {

  class Program

  {

  static void Main(string[] args)

  {

  Console.WriteLine($"start ThreadId: {Thread.CurrentThread.ManagedThreadId.ToString("00")}");

  //// ThreadPoll启动多线程

  //ThreadPool.QueueUserWorkItem(p => DoSomethingLong("启动多线程"));

  //// 获取最大线程

  //ThreadPool.GetMaxThreads(out int workerThreads, out int completionPortThreads);

  //Console.WriteLine($"GetMaxThreads workerThreads={workerThreads} completionPortThreads={completionPortThreads}");

  //// 获取最小线程

  //ThreadPool.GetMinThreads(out int minworkerThreads, out int mincompletionPortThreads);

  //Console.WriteLine($"GetMinThreads workerThreads={minworkerThreads} completionPortThreads={mincompletionPortThreads}");

  //// 设置线程池线程

  //SetThreadPool();

  //// 输出设置后的线程池线程个数

  //Console.WriteLine("输出修改后的最多线程数和最少线程数");

  //ThreadPool.GetMaxThreads(out int maxworkerThreads, out int maxcompletionPortThreads);

  //Console.WriteLine($"GetMaxThreads workerThreads={maxworkerThreads} completionPortThreads={maxcompletionPortThreads}");

  //ThreadPool.GetMinThreads(out int workerEditThreads, out int completionPortEditThreads);

  //Console.WriteLine($"GetMinThreads workerThreads={workerEditThreads} completionPortThreads={completionPortEditThreads}");

  //Console.WriteLine($"end ThreadId: {Thread.CurrentThread.ManagedThreadId.ToString("00")}");

  //// 参数设置为false

  //ManualResetEvent manualResetEvent = new ManualResetEvent(false);

  //ThreadPool.QueueUserWorkItem(p =>

  //{

  // DoSomethingLong("启动多线程");

  // // 设置为true

  // manualResetEvent.Set();

  //});

  ////

  //manualResetEvent.WaitOne();

  //Console.WriteLine("等着QueueUserWorkItem完成后才执行");

  // SetWait();

  // ThreadPool实现线程的重用

  // ThreadPoolTest();

  // Thread

  ThreadTest();

  Console.WriteLine($"end ThreadId: {Thread.CurrentThread.ManagedThreadId.ToString("00")}");

  Console.ReadKey();

  }

  static void DoSomethingLong(string para)

  {

  Console.WriteLine($"{para} ThreadId: {Thread.CurrentThread.ManagedThreadId.ToString("00")}");

  }

  ///

  /// 设置线程池线程个数

  ///

  static void SetThreadPool()

  {

  Console.WriteLine("************设置最多线程数和最少线程数****************");

  // 设置最大线程

  ThreadPool.SetMaxThreads(16, 16);

  // 设置最小线程

  ThreadPool.SetMinThreads(8, 8);

  }

  static void SetWait()

  {

  // 设置最大线程

  ThreadPool.SetMaxThreads(16, 16);

  // 设置最小线程

  ThreadPool.SetMinThreads(8, 8);

  ManualResetEvent manualResetEvent = new ManualResetEvent(false);

  for (int i = 0; i < 20; i++)

  {

  int k = i;

  ThreadPool.QueueUserWorkItem(p =>

  {

  Console.WriteLine(k);

  if (k < 18)

  {

  manualResetEvent.WaitOne();

  }

  else

  {

  // 设为true

  manualResetEvent.Set();

  }

  });

  }

  if (manualResetEvent.WaitOne())

  {

  Console.WriteLine("没有死锁、、、");

  }

  else

  {

  Console.WriteLine("发生死锁、、、");

  }

  }

  ///

  /// 测试ThreadPool线程重用

  ///

  static void ThreadPoolTest()

  {

  // 线程重用

  ThreadPool.QueueUserWorkItem(t => DoSomethingLong("ThreadPool"));

  ThreadPool.QueueUserWorkItem(t => DoSomethingLong("ThreadPool"));

  ThreadPool.QueueUserWorkItem(t => DoSomethingLong("ThreadPool"));

  ThreadPool.QueueUserWorkItem(t => DoSomethingLong("ThreadPool"));

  ThreadPool.QueueUserWorkItem(t => DoSomethingLong("ThreadPool"));

  Thread.Sleep(10 * 1000);

  Console.WriteLine("前面的计算都完成了。。。。。。。。");

  ThreadPool.QueueUserWorkItem(t => DoSomethingLong("ThreadPool"));

  ThreadPool.QueueUserWorkItem(t => DoSomethingLong("ThreadPool"));

  ThreadPool.QueueUserWorkItem(t => DoSomethingLong("ThreadPool"));

  ThreadPool.QueueUserWorkItem(t => DoSomethingLong("ThreadPool"));

  ThreadPool.QueueUserWorkItem(t => DoSomethingLong("ThreadPool"));

  }

  ///

  /// 测试Thread线程重用

  ///

  static void ThreadTest()

  {

  for (int i = 0; i < 5; i++)

  {

  new Thread(() => DoSomethingLong("Threads")).Start();

  }

  Thread.Sleep(10 * 1000);

  Console.WriteLine("前面的计算都完成了。。。。。。。。");

  for (int i = 0; i < 5; i++)

  {

  new Thread(() => DoSomethingLong("btnThreads")).Start();

  }

  }

  }

  }