Smart Thread Pool

太过爱你忘了你带给我的痛 2022-06-09 12:50 355阅读 0赞

demo:

https://www.codeproject.com/Articles/7933/Smart-Thread-Pool#SimpleExample

  1. // 创建一个线程池
  2. SmartThreadPool smartThreadPool = new SmartThreadPool();
  3. // 执行任务
  4. smartThreadPool.QueueWorkItem(() =>
  5. {
  6. Console.WriteLine("Hello World!");
  7. });
  8. // 创建一个线程池
  9. SmartThreadPool smartThreadPool = new SmartThreadPool();
  10. // 执行任务
  11. var result = smartThreadPool.QueueWorkItem(() =>
  12. {
  13. var sum = 0;
  14. for (var i = 0; i < 10; i++)
  15. sum += i;
  16. return sum;
  17. });
  18. // 输出计算结果
  19. Console.WriteLine(result.Result);
  20. 等待多个任务完成
  21. // 创建一个线程池
  22. SmartThreadPool smartThreadPool = new SmartThreadPool();
  23. // 执行任务
  24. var result1 = smartThreadPool.QueueWorkItem(() =>
  25. {
  26. //模拟计算较长时间
  27. Thread.Sleep(5000);
  28. return 3;
  29. });
  30. var result2 = smartThreadPool.QueueWorkItem(() =>
  31. {
  32. //模拟计算较长时间
  33. Thread.Sleep(3000);
  34. return 5;
  35. });
  36. bool success = SmartThreadPool.WaitAll(
  37. new IWorkItemResult[] { result1, result2 });
  38. if (success)
  39. {
  40. // 输出结果
  41. Console.WriteLine(result1.Result);
  42. Console.WriteLine(result2.Result);
  43. }

转载于:

https://www.codeproject.com/Articles/7933/Smart-Thread-Pool

http://blog.csdn.net/greystar/article/details/1766363

http://www.lingdonge.com/marketing/seo/37.html

发表评论

表情:
评论列表 (有 0 条评论,355人围观)

还没有评论,来说两句吧...

相关阅读