目录:

一、上节补充

二、CLR线程池基础

三、通过线程池的工作者线程实现异步

四、使用委托实现异步

五、任务

六、小结

一、上节补充

对于Thread类还有几个常用方法需要说明的。

1.1 Suspend和Resume方法

这两个方法在.net Framework 1.0 的时候就支持的方法,他们分别可以挂起线程和恢复挂起的线程。但在.net Framework 2.0以后的版本中这两个方法都过时了,MSDN的解释是这样:

警告:

不要使用 Suspend 和 Resume 方法来同步线程的活动。您无法知道挂起线程时它正在执行什么代码。如果您在安全权限评估期间挂起持有锁的线程,则 AppDomain中的其他线程可能被阻止。如果您在线程正在执行类构造函数时挂起它,则 AppDomain中尝试使用该类的其他线程将被阻止。这样很容易发生死锁。

对于这个解释可能有点抽象吧,让我们来看看一段代码可能会清晰点:

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. // 创建一个线程来测试
  6. Thread thread1 = new Thread(TestMethod);
  7. thread1.Name = "Thread1";
  8. thread1.Start();
  9. Thread.Sleep(2000);
  10. Console.WriteLine("Main Thread is running");
  11. int b = 0;
  12. int a = 3 / b;
  13. Console.WriteLine(a);
  14. thread1.Resume();
  15. Console.Read();
  16. }
  17. private static void TestMethod()
  18. {
  19. Console.WriteLine("Thread: {0} has been suspended!", Thread.CurrentThread.Name);
  20. //将当前线程挂起
  21. Thread.CurrentThread.Suspend();
  22. Console.WriteLine("Thread: {0} has been resumed!", Thread.CurrentThread.Name);
  23. }
  24. }
    在上面这段代码中thread1线程是在主线程中恢复的,但当主线程发生异常时,这时候就thread1一直处于挂起状态,此时thread1所使用的资源就不能释放(除非强制终止进程),当另外线程需要使用这快资源的时候, 这时候就很可能发生死锁现象。

上面一段代码还存在一个隐患,请看下面一小段代码:

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. // 创建一个线程来测试
  6. Thread thread1 = new Thread(TestMethod);
  7. thread1.Name = "Thread1";
  8. thread1.Start();
  9. Console.WriteLine("Main Thread is running");
  10. thread1.Resume();
  11. Console.Read();
  12. }
  13. private static void TestMethod()
  14. {
  15. Console.WriteLine("Thread: {0} has been suspended!", Thread.CurrentThread.Name);
  16. Thread.Sleep(1000);
  17. //将当前线程挂起
  18. Thread.CurrentThread.Suspend();
  19. Console.WriteLine("Thread: {0} has been resumed!", Thread.CurrentThread.Name);
  20. }
  21. }

当主线程跑(运行)的太快,做完自己的事情去唤醒thread1时,此时thread1还没有挂起而起唤醒thread1,此时就会出现异常了。并且上面使用的Suspend和Resume方法,编译器已经出现警告了,提示这两个方法已经过时, 所以在我们平时使用中应该尽量避免。

1.2 Abort和 Interrupt方法

Abort方法和Interrupt都是用来终止线程的,但是两者还是有区别的。

1、他们抛出的异常不一样,Abort 方法抛出的异常是ThreadAbortException, Interrupt抛出的异常为ThreadInterruptedException

2、调用interrupt方法的线程之后可以被唤醒,然而调用Abort方法的线程就直接被终止不能被唤醒的。

下面一段代码是演示Abort方法的使用

  1. using System;
  2. using System.Threading;
  3. namespace ConsoleApplication1
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. Thread abortThread = new Thread(AbortMethod);
  10. abortThread.Name = "Abort Thread";
  11. abortThread.Start();
  12. Thread.Sleep(1000);
  13. try
  14. {
  15. abortThread.Abort();
  16. }
  17. catch
  18. {
  19. Console.WriteLine("{0} Exception happen in Main Thread", Thread.CurrentThread.Name);
  20. Console.WriteLine("{0} Status is:{1} In Main Thread ", Thread.CurrentThread.Name, Thread.CurrentThread.ThreadState);
  21. }
  22. finally
  23. {
  24. Console.WriteLine("{0} Status is:{1} In Main Thread ", abortThread.Name, abortThread.ThreadState);
  25. }
  26. abortThread.Join();
  27. Console.WriteLine("{0} Status is:{1} ", abortThread.Name, abortThread.ThreadState);
  28. Console.Read();
  29. }
  30. private static void AbortMethod()
  31. {
  32. try
  33. {
  34. Thread.Sleep(5000);
  35. }
  36. catch(Exception e)
  37. {
  38. Console.WriteLine(e.GetType().Name);
  39. Console.WriteLine("{0} Exception happen In Abort Thread", Thread.CurrentThread.Name);
  40. Console.WriteLine("{0} Status is:{1} In Abort Thread ", Thread.CurrentThread.Name, Thread.CurrentThread.ThreadState);
  41. }
  42. finally
  43. {
  44. Console.WriteLine("{0} Status is:{1} In Abort Thread", Thread.CurrentThread.Name, Thread.CurrentThread.ThreadState);
  45. }
  46. }
  47. }
运行结果:

从运行结果可以看出,调用Abort方法的线程引发的异常类型为ThreadAbortException, 以及异常只会在 调用Abort方法的线程中发生,而不会在主线程中抛出,并且调用Abort方法后线程的状态不是立即改变为Aborted状态,而是从AbortRequested->Aborted。

Interrupt方法:

  1. using System;
  2. using System.Threading;
  3. namespace ConsoleApplication1
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. { Thread interruptThread = new Thread(AbortMethod);
  9. interruptThread.Name = "Interrupt Thread";
  10. interruptThread.Start();
  11. interruptThread.Interrupt();
  12. interruptThread.Join();
  13. Console.WriteLine("{0} Status is:{1} ", interruptThread.Name, interruptThread.ThreadState);
  14. Console.Read();
  15. }
  16. private static void AbortMethod()
  17. {
  18. try
  19. {
  20. Thread.Sleep(5000);
  21. }
  22. catch(Exception e)
  23. {
  24. Console.WriteLine(e.GetType().Name);
  25. Console.WriteLine("{0} Exception happen In Interrupt Thread", Thread.CurrentThread.Name);
  26. Console.WriteLine("{0} Status is:{1} In Interrupt Thread ", Thread.CurrentThread.Name, Thread.CurrentThread.ThreadState);
  27. }
  28. finally
  29. {
  30. Console.WriteLine("{0} Status is:{1} In Interrupt Thread", Thread.CurrentThread.Name, Thread.CurrentThread.ThreadState);
  31. }
  32. }
  33. }
  34. }
运行结果:

从结果中可以得到,调用Interrupt方法抛出的异常为:ThreadInterruptException, 以及当调用Interrupt方法后线程的状态应该是中断的, 但是从运行结果看此时的线程因为了Join,Sleep方法而唤醒了线程,为了进一步解释调用Interrupt方法的线程可以被唤醒, 我们可以在线程执行的方法中运用循环,如果线程可以唤醒,则输出结果中就一定会有循环的部分,然而调用Abort方法线程就直接终止,就不会有循环的部分,下面代码相信大家看后肯定会更加理解两个方法的区别的:

  1. using System;
  2. using System.Threading;
  3. namespace ConsoleApplication2
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. Thread thread1 = new Thread(TestMethod);
  10. thread1.Start();
  11. Thread.Sleep(100);
  12. thread1.Interrupt();
  13. Thread.Sleep(3000);
  14. Console.WriteLine("after finnally block, the Thread1 status is:{0}", thread1.ThreadState);
  15. Console.Read();
  16. }
  17. private static void TestMethod()
  18. {
  19. for (int i = 0; i < 4; i++)
  20. {
  21. try
  22. {
  23. Thread.Sleep(2000);
  24. Console.WriteLine("Thread is Running");
  25. }
  26. catch (Exception e)
  27. {
  28. if (e != null)
  29. {
  30. Console.WriteLine("Exception {0} throw ", e.GetType().Name);
  31. }
  32. }
  33. finally
  34. {
  35. Console.WriteLine("Current Thread status is:{0} ", Thread.CurrentThread.ThreadState);
  36. }
  37. }
  38. }
  39. }
  40. }
运行结果为:

如果把上面的 thread1.Interrupt();改为 thread1.Abort(); 运行结果为:

二、线程池基础

首先,创建和销毁线程是一个要耗费大量时间的过程,另外,太多的线程也会浪费内存资源,所以通过Thread类来创建过多的线程反而有损于性能,为了改善这样的问题 ,.net中就引入了线程池。

线程池形象的表示就是存放应用程序中使用的线程的一个集合(就是放线程的地方,这样线程都放在一个地方就好管理了)。CLR初始化时,线程池中是没有线程的,在内部, 线程池维护了一个操作请求队列,当应用程序想执行一个异步操作时,就调用一个方法,就将一个任务放到线程池的队列中,线程池中代码从队列中提取任务,将这个任务委派给一个线程池线程去执行,当线程池线程完成任务时,线程不会被销毁,而是返回到线程池中,等待响应另一个请求。由于线程不被销毁, 这样就可以避免因为创建线程所产生的性能损失。

注意:通过线程池创建的线程默认为后台线程,优先级默认为Normal.

三、通过线程池的工作者线程实现异步

3.1 创建工作者线程的方法

public static bool QueueUserWorkItem (WaitCallback callBack);

public static bool QueueUserWorkItem(WaitCallback callback, Object state);

这两个方法向线程池的队列添加一个工作项(work item)以及一个可选的状态数据。然后,这两个方法就会立即返回。

工作项其实就是由callback参数标识的一个方法,该方法将由线程池线程执行。同时写的回调方法必须匹配System.Threading.WaitCallback委托类型,定义为:

public delegate void WaitCallback(Object state);

下面演示如何通过线程池线程来实现异步调用:

  1. using System;
  2. using System.Threading;
  3. namespace ThreadPoolUse
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. // 设置线程池中处于活动的线程的最大数目
  10. // 设置线程池中工作者线程数量为1000,I/O线程数量为1000
  11. ThreadPool.SetMaxThreads(1000, 1000);
  12. Console.WriteLine("Main Thread: queue an asynchronous method");
  13. PrintMessage("Main Thread Start");
  14. // 把工作项添加到队列中,此时线程池会用工作者线程去执行回调方法
  15. ThreadPool.QueueUserWorkItem(asyncMethod);
  16. Console.Read();
  17. }
  18. // 方法必须匹配WaitCallback委托
  19. private static void asyncMethod(object state)
  20. {
  21. Thread.Sleep(1000);
  22. PrintMessage("Asynchoronous Method");
  23. Console.WriteLine("Asynchoronous thread has worked ");
  24. }
  25. // 打印线程池信息
  26. private static void PrintMessage(String data)
  27. {
  28. int workthreadnumber;
  29. int iothreadnumber;
  30. // 获得线程池中可用的线程,把获得的可用工作者线程数量赋给workthreadnumber变量
  31. // 获得的可用I/O线程数量给iothreadnumber变量
  32. ThreadPool.GetAvailableThreads(out workthreadnumber, out iothreadnumber);
  33. Console.WriteLine("{0}\n CurrentThreadId is {1}\n CurrentThread is background :{2}\n WorkerThreadNumber is:{3}\n IOThreadNumbers is: {4}\n",
  34. data,
  35. Thread.CurrentThread.ManagedThreadId,
  36. Thread.CurrentThread.IsBackground.ToString(),
  37. workthreadnumber.ToString(),
  38. iothreadnumber.ToString());
  39. }
  40. }
  41. }
运行结果:   

从结果中可以看出,线程池中的可用的工作者线程少了一个,用去执行回调方法了。

ThreadPool.QueueUserWorkItem(WaitCallback callback,Object state) 方法可以把object对象作为参数传送到回调函数中,使用和ThreadPool.QueueUserWorkItem(WaitCallback callback)的使用和类似,这里就不列出了。

3.2 协作式取消

.net Framework提供了取消操作的模式, 这个模式是协作式的。为了取消一个操作,首先必须创建一个System.Threading.CancellationTokenSource对象。

下面代码演示了协作式取消的使用,主要实现当用户在控制台敲下回车键后就停止数数方法。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. namespace ConsoleApplication3
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. ThreadPool.SetMaxThreads(1000, 1000);
  13. Console.WriteLine("Main thread run");
  14. PrintMessage("Start");
  15. Run();
  16. Console.ReadKey();
  17. }
  18. private static void Run()
  19. {
  20. CancellationTokenSource cts = new CancellationTokenSource();
  21. // 这里用Lambda表达式的方式和使用委托的效果一样的,只是用了Lambda后可以少定义一个方法。
  22. // 这在这里就是让大家明白怎么lambda表达式如何由委托转变的
  23. ThreadPool.QueueUserWorkItem(o => Count(cts.Token, 1000));
  24. ThreadPool.QueueUserWorkItem(callback, cts.Token);
  25. Console.WriteLine("Press Enter key to cancel the operation\n");
  26. Console.ReadLine();
  27. // 传达取消请求
  28. cts.Cancel();
  29. }
  30. private static void callback(object state)
  31. {
  32. Thread.Sleep(1000);
  33. PrintMessage("Asynchoronous Method Start");
  34. CancellationToken token =(CancellationToken)state;
  35. Count(token, 1000);
  36. }
  37. // 执行的操作,当受到取消请求时停止数数
  38. private static void Count(CancellationToken token,int countto)
  39. {
  40. for (int i = 0; i < countto; i++)
  41. {
  42. if (token.IsCancellationRequested)
  43. {
  44. Console.WriteLine("Count is canceled");
  45. break;
  46. }
  47. Console.WriteLine(i);
  48. Thread.Sleep(300);
  49. }
  50. Console.WriteLine("Cout has done");
  51. }
  52. // 打印线程池信息
  53. private static void PrintMessage(String data)
  54. {
  55. int workthreadnumber;
  56. int iothreadnumber;
  57. // 获得线程池中可用的线程,把获得的可用工作者线程数量赋给workthreadnumber变量
  58. // 获得的可用I/O线程数量给iothreadnumber变量
  59. ThreadPool.GetAvailableThreads(out workthreadnumber, out iothreadnumber);
  60. Console.WriteLine("{0}\n CurrentThreadId is {1}\n CurrentThread is background :{2}\n WorkerThreadNumber is:{3}\n IOThreadNumbers is: {4}\n",
  61. data,
  62. Thread.CurrentThread.ManagedThreadId,
  63. Thread.CurrentThread.IsBackground.ToString(),
  64. workthreadnumber.ToString(),
  65. iothreadnumber.ToString());
  66. }
  67. }
  68. }
运行结果:

四、使用委托实现异步

通过调用ThreadPool的QueueUserWorkItem方法来来启动工作者线程非常方便,但委托WaitCallback指向的是带有一个参数的无返回值的方法,如果我们实际操作中需要有返回值,或者需要带有多个参数, 这时通过这样的方式就难以实现, 为了解决这样的问题,我们可以通过委托来建立工作这线程,

下面代码演示了使用委托如何实现异步:

  1. using System;
  2. using System.Threading;
  3. namespace Delegate
  4. {
  5. class Program
  6. {
  7. // 使用委托的实现的方式是使用了异步变成模型APM(Asynchronous Programming Model)
  8. // 自定义委托
  9. private delegate string MyTestdelegate();
  10. static void Main(string[] args)
  11. {
  12. ThreadPool.SetMaxThreads(1000, 1000);
  13. PrintMessage("Main Thread Start");
  14. //实例化委托
  15. MyTestdelegate testdelegate = new MyTestdelegate(asyncMethod);
  16. // 异步调用委托
  17. IAsyncResult result = testdelegate.BeginInvoke(null, null);
  18. // 获取结果并打印出来
  19. string returndata = testdelegate.EndInvoke(result);
  20. Console.WriteLine(returndata);
  21. Console.ReadLine();
  22. }
  23. private static string asyncMethod()
  24. {
  25. Thread.Sleep(1000);
  26. PrintMessage("Asynchoronous Method");
  27. return "Method has completed";
  28. }
  29. // 打印线程池信息
  30. private static void PrintMessage(String data)
  31. {
  32. int workthreadnumber;
  33. int iothreadnumber;
  34. // 获得线程池中可用的线程,把获得的可用工作者线程数量赋给workthreadnumber变量
  35. // 获得的可用I/O线程数量给iothreadnumber变量
  36. ThreadPool.GetAvailableThreads(out workthreadnumber, out iothreadnumber);
  37. Console.WriteLine("{0}\n CurrentThreadId is {1}\n CurrentThread is background :{2}\n WorkerThreadNumber is:{3}\n IOThreadNumbers is: {4}\n",
  38. data,
  39. Thread.CurrentThread.ManagedThreadId,
  40. Thread.CurrentThread.IsBackground.ToString(),
  41. workthreadnumber.ToString(),
  42. iothreadnumber.ToString());
  43. }
  44. }
  45. }
运行结果:

五、任务

同样 任务的引入也是为了解决通过ThreadPool.QueueUserWorkItem中限制的问题,

下面代码演示通过任务来实现异步:

5.1 使用任务来实现异步

  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. namespace TaskUse
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. ThreadPool.SetMaxThreads(1000, 1000);
  11. PrintMessage("Main Thread Start");
  12. // 调用构造函数创建Task对象,
  13. Task<int> task = new Task<int>(n => asyncMethod((int)n), 10);
  14. // 启动任务
  15. task.Start();
  16. // 等待任务完成
  17. task.Wait();
  18. Console.WriteLine("The Method result is: "+task.Result);
  19. Console.ReadLine();
  20. }
  21. private static int asyncMethod(int n)
  22. {
  23. Thread.Sleep(1000);
  24. PrintMessage("Asynchoronous Method");
  25. int sum = 0;
  26. for (int i = 1; i < n; i++)
  27. {
  28. // 如果n太大,使用checked使下面代码抛出异常
  29. checked
  30. {
  31. sum += i;
  32. }
  33. }
  34. return sum;
  35. }
  36. // 打印线程池信息
  37. private static void PrintMessage(String data)
  38. {
  39. int workthreadnumber;
  40. int iothreadnumber;
  41. // 获得线程池中可用的线程,把获得的可用工作者线程数量赋给workthreadnumber变量
  42. // 获得的可用I/O线程数量给iothreadnumber变量
  43. ThreadPool.GetAvailableThreads(out workthreadnumber, out iothreadnumber);
  44. Console.WriteLine("{0}\n CurrentThreadId is {1}\n CurrentThread is background :{2}\n WorkerThreadNumber is:{3}\n IOThreadNumbers is: {4}\n",
  45. data,
  46. Thread.CurrentThread.ManagedThreadId,
  47. Thread.CurrentThread.IsBackground.ToString(),
  48. workthreadnumber.ToString(),
  49. iothreadnumber.ToString());
  50. }
  51. }
  52. }
运行结果:

5.2 取消任务

如果要取消任务, 同样可以使用一个CancellationTokenSource对象来取消一个Task.

下面代码演示了如何来取消一个任务:

  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. namespace TaskUse
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. ThreadPool.SetMaxThreads(1000, 1000);
  11. PrintMessage("Main Thread Start");
  12. CancellationTokenSource cts = new CancellationTokenSource();
  13. // 调用构造函数创建Task对象,将一个CancellationToken传给Task构造器从而使Task和CancellationToken关联起来
  14. Task<int> task = new Task<int>(n => asyncMethod(cts.Token, (int)n), 10);
  15. // 启动任务
  16. task.Start();
  17. // 延迟取消任务
  18. Thread.Sleep(3000);
  19. // 取消任务
  20. cts.Cancel();
  21. Console.WriteLine("The Method result is: " + task.Result);
  22. Console.ReadLine();
  23. }
  24. private static int asyncMethod(CancellationToken ct, int n)
  25. {
  26. Thread.Sleep(1000);
  27. PrintMessage("Asynchoronous Method");
  28. int sum = 0;
  29. try
  30. {
  31. for (int i = 1; i < n; i++)
  32. {
  33. // 当CancellationTokenSource对象调用Cancel方法时,
  34. // 就会引起OperationCanceledException异常
  35. // 通过调用CancellationToken的ThrowIfCancellationRequested方法来定时检查操作是否已经取消,
  36. // 这个方法和CancellationToken的IsCancellationRequested属性类似
  37. ct.ThrowIfCancellationRequested();
  38. Thread.Sleep(500);
  39. // 如果n太大,使用checked使下面代码抛出异常
  40. checked
  41. {
  42. sum += i;
  43. }
  44. }
  45. }
  46. catch (Exception e)
  47. {
  48. Console.WriteLine("Exception is:" + e.GetType().Name);
  49. Console.WriteLine("Operation is Canceled");
  50. }
  51. return sum;
  52. }
  53. // 打印线程池信息
  54. private static void PrintMessage(String data)
  55. {
  56. int workthreadnumber;
  57. int iothreadnumber;
  58. // 获得线程池中可用的线程,把获得的可用工作者线程数量赋给workthreadnumber变量
  59. // 获得的可用I/O线程数量给iothreadnumber变量
  60. ThreadPool.GetAvailableThreads(out workthreadnumber, out iothreadnumber);
  61. Console.WriteLine("{0}\n CurrentThreadId is {1}\n CurrentThread is background :{2}\n WorkerThreadNumber is:{3}\n IOThreadNumbers is: {4}\n",
  62. data,
  63. Thread.CurrentThread.ManagedThreadId,
  64. Thread.CurrentThread.IsBackground.ToString(),
  65. workthreadnumber.ToString(),
  66. iothreadnumber.ToString());
  67. }
  68. }
  69. }
运行结果:

5.3 任务工厂

同样可以通过任务工厂TaskFactory类型来实现异步操作。

  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. namespace TaskFactory
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. ThreadPool.SetMaxThreads(1000, 1000);
  11. Task.Factory.StartNew(() => PrintMessage("Main Thread"));
  12. Console.Read();
  13. }
  14. // 打印线程池信息
  15. private static void PrintMessage(String data)
  16. {
  17. int workthreadnumber;
  18. int iothreadnumber;
  19. // 获得线程池中可用的线程,把获得的可用工作者线程数量赋给workthreadnumber变量
  20. // 获得的可用I/O线程数量给iothreadnumber变量
  21. ThreadPool.GetAvailableThreads(out workthreadnumber, out iothreadnumber);
  22. Console.WriteLine("{0}\n CurrentThreadId is {1}\n CurrentThread is background :{2}\n WorkerThreadNumber is:{3}\n IOThreadNumbers is: {4}\n",
  23. data,
  24. Thread.CurrentThread.ManagedThreadId,
  25. Thread.CurrentThread.IsBackground.ToString(),
  26. workthreadnumber.ToString(),
  27. iothreadnumber.ToString());
  28. }
  29. }
  30. }
运行结果:

六、小结

讲到这里CLR的工作者线程大致讲完了,希望也篇文章可以让大家对线程又有进一步的理解。在后面的一篇线程系列将谈谈CLR线程池的I/O线程。

[.Net线程处理系列]专题二:线程池中的工作者线程相关推荐

  1. 突然就懵了!面试官问我:线程池中多余的线程是如何回收的?

    点击关注公众号,Java干货及时送达 最近阅读了JDK线程池ThreadPoolExecutor的源码,对线程池执行任务的流程有了大体了解,实际上这个流程也十分通俗易懂,就不再赘述了,别人写的比我好多 ...

  2. C#如何判断线程池中所有的线程是否已经完成(转)

    其 实很简单用ThreadPool.RegisterWaitForSingleObject方法注册一个定时检查线程池的方法,在检查线程的方法内调用 ThreadPool.GetAvailableThr ...

  3. [C#]获得线程池中活动的线程数

    在C#中的线程池ThreadPool没有获得线程池中的活动线程数量的属性或者是方法,但是有一两个 方法可以帮助获得活动线程数. 这两个方法分别是:GetMaxThreads和GetAvailableT ...

  4. [.NET领域驱动设计实战系列]专题二:结合领域驱动设计的面向服务架构来搭建网上书店...

    原文:[.NET领域驱动设计实战系列]专题二:结合领域驱动设计的面向服务架构来搭建网上书店 一.前言 在前面专题一中,我已经介绍了我写这系列文章的初衷了.由于dax.net中的DDD框架和Bytear ...

  5. Java线程池线程突然没了_70%人答不全!线程池中的一个线程异常了会被怎么处理?...

    #线程池中的一个线程异常了会被怎么处理? 估计很多人会是以下三点答案(me too): 1.抛异常出来并打印在控制台上 2.其他线程任务不受影响 3.异常线程会被回收 但是这里我先提前说一下以上三点不 ...

  6. 线程池中运行的线程,当等待队列未满的情况下,一定不大于核心线程数吗

    通过<线程池内运行的线程抛异常,线程池会怎么办>了解到当线程执行的任务出现异常时,会将当前线程移出线程池,并新增一个线程到线程池中,我们先来回顾一下线程池的运行原理: 从原理图中可以看到只 ...

  7. [.Net线程处理系列]专题五:线程同步——事件构造

    引言: 其实这部分内容应该是属于专题四,因为这篇也是讲关于线程同步的,但是由于考虑到用户的阅读习惯问题,因为文章太长了,很多人不是很愿意看包括我也是这样的,同时也有和我说可以把代码弄成折叠的,这样就不 ...

  8. [C# 线程处理系列]专题四:线程同步

    目录: 一.线程同步概述 二.线程同步的使用 三 .总结 一.线程同步概述 前面的文章都是讲创建多线程来实现让我们能够更好的响应应用程序,然而当我们创建了多个线程时,就存在多个线程同时访问一个共享的资 ...

  9. Android-JNI开发系列《二》-在jni层的线程中回调到java层

    人间观察 忽有故人心上头,回首山河已是秋. 马上国庆+中秋了--- 今天我们看一个比较常见的场景: 当我们处理一个密集型计算数据(比如音视频的软编解码处理,bitmap的特效处理等),这时候就需要用c ...

最新文章

  1. maven学习(中)- 私服nexus搭建
  2. firefox扩展开发(二):用XUL创建窗口控件
  3. ABAP性能优化之使用 “for all entries”
  4. 对jquery的conflict方法的解读
  5. 数据结构学习笔记(一)——《大话数据结构》
  6. 一些比较有用的SQL操作,2011最新整理
  7. Sharding-JDBC水平分表(环境搭建)_Sharding-Sphere,Sharding-JDBC分布式_分库分表工作笔记007
  8. 【React-2】组件开发初步探索
  9. 在线JSON转Excel工具
  10. 如何接入易班第三方登录
  11. 玄武中专计算机动漫与游戏制作,正青春,不散场——玄武中专信息技术系举办2016级计算机动漫与游戏制作专业“3+3”学生毕业作品展开幕式...
  12. 量子计算机游戏,第3章 量子计算机中的游戏
  13. [数学基础知识] Cramér‘s V 相关系数和Python算法实现
  14. C PRIMER PLUS第七章11题
  15. 安装类Excel开发工具设计器
  16. MingW下载与安装
  17. 3D动画旋转rotateY的用法(23)
  18. python爬虫开发微课版pdf_Python爬虫开发实战教程(微课版)
  19. Python 的reload()方法
  20. KMP讲解(自制动图)

热门文章

  1. View事件分发机制(源码 API27)
  2. 使用maven创建项目和cannot change version web module 3.0
  3. Java面向对象编程思想
  4. django基本操作
  5. JavaScript 日期联动选择器
  6. 在AcGIS随着大数据的生成DEM
  7. fancybox 在打开窗口前 先执行 js脚本,fancybox设置只能通过右上角关闭,fancybox遮罩层关闭...
  8. 查看JAVA源码的网站
  9. 在Laravel外使用Eloquent(一)
  10. 625某电商网站数据库宕机故障解决实录(下)