Thread.join与ThreadPool

简介: 1、Thread.join()    使用此方法先阻塞调用Thread的线程,确保线程Thread正常终止。     如果线程不终止,则调用方将无限期阻塞。如果调用 Join 时该线程已终止,此方法将立即返回。

1、Thread.join()

   使用此方法先阻塞调用Thread的线程,确保线程Thread正常终止。

    如果线程不终止,则调用方将无限期阻塞。如果调用 Join 时该线程已终止,此方法将立即返回。

此方法更改调用线程的状态以包括 ThreadState..::.WaitSleepJoin。对处于 ThreadState..::.Unstarted 状态的线程不能调用 Join。

 

案例代码分析:

  普通线程的创建,通过 委托ThreadStart对应的函数来执行相关操作;

  通过线程池,可以直接从池中查找出空闲线程,让它执行委托WaitCallback对应函数来执行相关操作。使用时要与AutoResetEvent来并用,以在线程结束时通知主线程退出;

 


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;

  6. namespace joinDemo
  7. {
  8.     class Program
  9.     {
  10.         static void Main()
  11.         {
  12.             /* 通知正在等待的线程已经发生事件 */
  13.             AutoResetEvent autoEvent = new AutoResetEvent(false);

  14.             Thread regularThread =
  15.                 new Thread(new ThreadStart(ThreadMethod));
  16.             regularThread.Start();

  17.             /*
  18.              * 提供一个线程池,该线程池可用于发送工作项、处理异步 I/O、
  19.              * 代表其他线程等待以及处理计时器。
  20.              */
  21.             ThreadPool.QueueUserWorkItem(new WaitCallback(WorkMethod),
  22.                 autoEvent);

  23.             /* Wait for foreground thread to end. 一直阻塞 */
  24.             regularThread.Join();

  25.             /* Wait for background thread to end. 主线程使用WaitOne来等待解除线程 */
  26.             autoEvent.WaitOne();
  27.             Console.ReadLine();
  28.         }

  29.         static void ThreadMethod()
  30.         {
  31.             Console.WriteLine("ThreadOne, executing ThreadMethod, " +
  32.                 "is {0}from the thread pool.",
  33.                 Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");
  34.         }

  35.         static void WorkMethod(object stateInfo)
  36.         {
  37.             Console.WriteLine("ThreadTwo, executing WorkMethod, " +
  38.                 "is {0}from the thread pool.",
  39.                 Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");

  40.             /* Signal that this thread is finished.事件通知,解除线程阻塞 */
  41.             ((AutoResetEvent)stateInfo).Set();
  42.         }

  43.     }
  44. }


image

 

 

2、ThreadPool类

ThreadPool类提供一个线程池,该线程池可用于发送工作项、处理异步 I/O、代表其他线程等待以及处理计时器。

案例分析:

     在本案例中,使用线程池来找到一个线程执行函数,实质减少了new thread等过程。


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;

  6. namespace ThreadPoolDemo
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             // Queue the task.
  13.             /* 请求线程池中的空闲一个线程来处理工作项 */
  14.             ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));

  15.             Console.WriteLine("Main thread does some work, then sleeps.");
  16.             /*
  17.              * If you comment out(注释掉) the Sleep, the main thread exits before
  18.              * the thread pool task runs. The thread pool uses background
  19.              * threads, which do not keep the application running,即main主线程退出后,
  20.              * 其它线程不会再执行. (This
  21.              * is a simple example of a race condition.)
  22.              */
  23.             Thread.Sleep(1000);

  24.             Console.WriteLine("Main thread exits.");
  25.             Console.ReadLine();
  26.         }

  27.         ///
  28.         /// This thread procedure performs the task.
  29.         ///
  30.         ///
  31.         static void ThreadProc(Object stateInfo)
  32.         {
  33.             /*
  34.              *
  35.              * No state object was passed to QueueUserWorkItem, so
  36.              * stateInfo is null.
  37.              */
  38.             Console.WriteLine("Hello from the thread pool.");

  39.         }
  40.     }
  41. }

 

image

相关文章
|
6月前
|
设计模式 Java 调度
多案例理解Object的wait,notify,notifyAll与Thread的sleep,yield,join等方法
多案例理解Object的wait,notify,notifyAll与Thread的sleep,yield,join等方法
86 1
|
4月前
|
Java C# Python
线程等待(Thread Sleep)
线程等待(Thread Sleep)
|
5月前
|
Java 编译器 UED
Thread.sleep()总结
Thread.sleep()总结
|
6月前
|
Python
thread.join()
thread.join()
42 1
|
6月前
|
监控 算法 Unix
Thread.sleep(0) 到底有什么用
Thread.sleep(0) 到底有什么用
54 1
Thread
Thread方法
65 0
|
调度 C++
Thread.sleep(0) vs Thread.sleep(1) vs Thread.yield() vs Object.wait()
Thread.sleep(0) vs Thread.sleep(1) vs Thread.yield() vs Object.wait()
|
Java 调度 C++
你真的了解Thread.sleep(0)吗?以及Thread.sleep(1) vs Thread.sleep(0)
你真的了解Thread.sleep(0)吗?以及Thread.sleep(1) vs Thread.sleep(0)
|
Java Linux 调度
Thread.yield、Thread.sleep、Object.wait、 LockSupport.park 对比
Thread.yield、Thread.sleep、Object.wait、 LockSupport.park 对比
451 0
Thread.yield、Thread.sleep、Object.wait、 LockSupport.park 对比