注:要使用此方法都需要引入应用:using System.Threading;//引入应用

参数意义:将要执行的方法排入队列以便执行,WaitCallback,即表示将要执行的方法;Object,包含方法所用数据的对象。如果将方法成功排入队列,则为 true;否则为 false

一、下面是ThreadPool.QueueUserWorkItem 方法 (WaitCallback)的示例代码:

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading;//引入应用
6
7 namespace QueueUserWorkItem_WaitCallback_
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 // Queue the task.
14 ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
15
16 Console.WriteLine("Main thread does some work, then sleeps.");
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. (This
20 // is a simple example of a race condition.)
21 Thread.Sleep(1000);
22
23 Console.WriteLine("Main thread exits.");
24 }
25
26 // This thread procedure performs the task.
27 static void ThreadProc(Object stateInfo)
28 {
29 // No state object was passed to QueueUserWorkItem, so
30 // stateInfo is null.
31 Console.WriteLine("Hello from the thread pool.");
32 }
33 }
34 }

运行以上代码后:

二、下面是ThreadPool.QueueUserWorkItem 方法 (WaitCallback, Object)的示例代码:

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading;
6
7 namespace QueueUserWorkItem__WaitCallback__Object_
8 {
9 //将方法排入队列以便执行,并指定包含该方法所用数据的对象。 此方法在有线程池线程变得可用时执行。
10 /*
11 *
12 * public static bool QueueUserWorkItem(WaitCallback callBack,Object state)
13 *
14 *
15 * callBack 类型:System.Threading.WaitCallback
16 * WaitCallback ,它表示要执行的方法。
17 *
18 *
19 * state 类型:System.Object
20 * 包含方法所用数据的对象。
21 *
22 *
23 * 返回值 类型:System.Boolean
24 * 如果此方法成功排队,则为 true;如果无法将该工作项排队,则引发 NotSupportedException。
25 *
26 *
27 * 注:如果回调方法需要复杂数据,可以定义包含这些数据的类。
28 *
29 */
30 class Program
31 {
32 static void Main(string[] args)
33 {
34 // Create an object containing the information needed for the task.
35 TaskInfo ti = new TaskInfo("This report displays the number {0}.", 42);
36
37 // Queue the task and data.
38 ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), ti);
39
40 Console.WriteLine("Main thread does some work, then sleeps.");
41
42 // If you comment out the Sleep, the main thread exits before
43 // the ThreadPool task has a chance to run. ThreadPool uses
44 // background threads, which do not keep the application
45 // running. (This is a simple example of a race condition.)
46 Thread.Sleep(1000);
47
48 Console.WriteLine("Main thread exits.");
49 }
50
51 // The thread procedure performs the independent task, in this case
52 // formatting and printing a very simple report.
53 //
54 static void ThreadProc(Object stateInfo)
55 {
56 TaskInfo ti = (TaskInfo)stateInfo;
57 Console.WriteLine(ti.Boilerplate, ti.Value);
58 }
59 }
60
61 // TaskInfo holds state information for a task that will be
62 // executed by a ThreadPool thread.
63 public class TaskInfo
64 {
65 // State information for the task. These members
66 // can be implemented as read-only properties, read/write
67 // properties with validation, and so on, as required.
68 public string Boilerplate;
69 public int Value;
70
71 // Public constructor provides an easy way to supply all
72 // the information needed for the task.
73 public TaskInfo(string text, int number)
74 {
75 Boilerplate = text;
76 Value = number;
77 }
78 }
79 }

运行以上代码后:

C#之线程和线程池(Thread和ThreadPool类)的更多相关文章

  1. C#中Thread与ThreadPool的比较

    最近同事在编写一个基于UPD RTP协议的通信软件,在处理接收Listen时,发现了一个问题到底是用Thread还是ThreadPool呢? 我看同事的问题比较有典型性,还是做以整理培训一下吧 Thr ...

  2. Thread and ThreadPool

    C#中Thread与ThreadPool的比较 Thread类,一次使用一个线程,来创建和删除线程.这种方式建立和删除线程是很昂贵的(cpu密集型). Threadpool类 对于大多数的情况下是使用 ...

  3. 通过Thread Pool Executor类解析线程池执行任务的核心流程

    摘要:ThreadPoolExecutor是Java线程池中最核心的类之一,它能够保证线程池按照正常的业务逻辑执行任务,并通过原子方式更新线程池每个阶段的状态. 本文分享自华为云社区<[高并发] ...

  4. 线程和线程池的理解与java简单例子

    1.线程 (1)理解,线程是系统分配处理器时间资源的基本单元也是系统调用的基本单位,简单理解就是一个或多个线程组成了一个进程,进程就像爸爸,线程就像儿子,有时候爸爸一个人干不了活就生了几个儿子干活,会 ...

  5. [Java线程] Java线程池ExecutorService

    示例 import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.u ...

  6. android线程与线程池-----线程池(二)《android开发艺术与探索》

    android 中的线程池 线程池的优点: 1 重用线程池中的线程,避免了线程的创建和销毁带来的性能开销 2 能有效的控制最大并发数,避免大量线程之间因为喜欢抢资源而导致阻塞 3 能够对线程进行简单的 ...

  7. Http协议、线程、线程池

    Socket模拟服务端运行代码: 1:启动服务端监听的服务,并接受客户端的连接 1.1 创建Socket Socket listenSocket=new Socket(AddressFamily.In ...

  8. 《Android开发艺术探索》读书笔记 (11) 第11章 Android的线程和线程池

    第11章 Android的线程和线程池 11.1 主线程和子线程 (1)在Java中默认情况下一个进程只有一个线程,也就是主线程,其他线程都是子线程,也叫工作线程.Android中的主线程主要处理和界 ...

  9. 线程池之ThreadPool类与辅助线程 - <第二篇>

    一.CLR线程池 管理线程开销最好的方式: 尽量少的创建线程并且能将线程反复利用(线程池初始化时没有线程,有程序请求线程则创建线程): 最好不要销毁而是挂起线程达到避免性能损失(线程池创建的线程完成任 ...

随机推荐

  1. [ USACO 2017 FEB ] Why Did the Cow Cross the Road III (Gold)

    \(\\\) \(Description\) 给定长度为\(2N\)的序列,\(1\text ~N\)各出现过\(2\)次,\(i\)第一次出现位置记为\(a_i\),第二次记为\(b_i\),求满足 ...

  2. CF 334 div.2-D Moodular Arithmetic

    思路: 易知k = 0的时候答案是pp-1,k = 1的时候答案是pp. 当k >= 2的时候,f(0) = 0,对于 1 <= n <= p - 1,如果f(n)确定,由题意可知f ...

  3. 为什么字符串类型可以调用构造函数String的方法,却又不是它的实例

    从所周知,在js中定义一个字符串我们有两种办法: var a = new String("a"); var a = "a"; 第一种方法使用构造函数创建,作为S ...

  4. 仿ofo单车做一个轮播效果

    github地址 首先我是利用swiper.js做的,因为这个很强大,哈哈~~,上代码 html很简单 <body> <div class="swiper-containe ...

  5. 解决安装androidstudio无法查看源代码的问题

    如果androidstudio的sdk是自己导入的,则可能会有查看不了源代码的原因.原因是默认目录中没有这个api的源代码. 1.先在C:\Users\xxx\.AndroidStudio2.3\co ...

  6. TensorFlow车牌识别实践(1)

    本文对公开的文章进行验证,从环境搭建到运行到结果分析. 1,文章:基于TensorFlow的车牌号识别系统 文章(译文) http://www.cnblogs.com/Jsmile2017/p/680 ...

  7. acedssget F 方式

    ads_point p1; ads_point p2; acedGetPoint(NULL, _T("\n插入第一点"), p1); acedGetPoint(p1, _T(&qu ...

  8. Oracle 把一个用户所有表的读权限授予另一个用户

    create user <USER_NAME> identified by <PASSWORD>; grant create session TO <USER_NAME& ...

  9. Compute和Linq的Field使用

    目录: Compute的使用 Field的使用 1.Compute 案例: private void ComputeBySalesSalesID(DataSet dataSet) { // Presu ...

  10. 解决移动端 footer fixd 定位被键盘顶起来的方案

    直接上代码: $(document).ready(function () { var u = navigator.userAgent; var isAndroid = u.indexOf('Andro ...