C#之线程和线程池(Thread和ThreadPool类)
注:要使用此方法都需要引入应用: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类)的更多相关文章
- C#中Thread与ThreadPool的比较
最近同事在编写一个基于UPD RTP协议的通信软件,在处理接收Listen时,发现了一个问题到底是用Thread还是ThreadPool呢? 我看同事的问题比较有典型性,还是做以整理培训一下吧 Thr ...
- Thread and ThreadPool
C#中Thread与ThreadPool的比较 Thread类,一次使用一个线程,来创建和删除线程.这种方式建立和删除线程是很昂贵的(cpu密集型). Threadpool类 对于大多数的情况下是使用 ...
- 通过Thread Pool Executor类解析线程池执行任务的核心流程
摘要:ThreadPoolExecutor是Java线程池中最核心的类之一,它能够保证线程池按照正常的业务逻辑执行任务,并通过原子方式更新线程池每个阶段的状态. 本文分享自华为云社区<[高并发] ...
- 线程和线程池的理解与java简单例子
1.线程 (1)理解,线程是系统分配处理器时间资源的基本单元也是系统调用的基本单位,简单理解就是一个或多个线程组成了一个进程,进程就像爸爸,线程就像儿子,有时候爸爸一个人干不了活就生了几个儿子干活,会 ...
- [Java线程] Java线程池ExecutorService
示例 import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.u ...
- android线程与线程池-----线程池(二)《android开发艺术与探索》
android 中的线程池 线程池的优点: 1 重用线程池中的线程,避免了线程的创建和销毁带来的性能开销 2 能有效的控制最大并发数,避免大量线程之间因为喜欢抢资源而导致阻塞 3 能够对线程进行简单的 ...
- Http协议、线程、线程池
Socket模拟服务端运行代码: 1:启动服务端监听的服务,并接受客户端的连接 1.1 创建Socket Socket listenSocket=new Socket(AddressFamily.In ...
- 《Android开发艺术探索》读书笔记 (11) 第11章 Android的线程和线程池
第11章 Android的线程和线程池 11.1 主线程和子线程 (1)在Java中默认情况下一个进程只有一个线程,也就是主线程,其他线程都是子线程,也叫工作线程.Android中的主线程主要处理和界 ...
- 线程池之ThreadPool类与辅助线程 - <第二篇>
一.CLR线程池 管理线程开销最好的方式: 尽量少的创建线程并且能将线程反复利用(线程池初始化时没有线程,有程序请求线程则创建线程): 最好不要销毁而是挂起线程达到避免性能损失(线程池创建的线程完成任 ...
随机推荐
- TensorFlow车牌识别实践(1)
本文对公开的文章进行验证,从环境搭建到运行到结果分析. 1,文章:基于TensorFlow的车牌号识别系统 文章(译文) http://www.cnblogs.com/Jsmile2017/p/680 ...
- 【C++】智能指针简述(四):shared_ptr
在开始本文内容之前,我们再来总结一下,前文内容: 1.智能指针采用RAII机制,在构造对象时进行资源的初始化,析构对象时进行资源的清理及汕尾. 2.auto_ptr防止拷贝后析构释放同一块内存,采用& ...
- ajax的底层前后台交互
为什么用ajax或者它的优点: 异步加载数据,无需切换页面 更加的用户体验,局部刷新,及时验证,操作步骤简化: 节省流量 js控制数据的加载,更加灵活多用. 底层就是XMLHttpRequest对象: ...
- 浏览器的两种模式quirks mode 和strict mode
关键字: javascript.quirks mode.strict mode 在看js代码时,有时会看到关于quirks mode(怪异模式)和strict mode(严格格式)的东西,一直也没深究 ...
- Ubuntu搭建LAMP开发环境
1.安装Apache sudo apt-get install apache2 测试: 浏览器访问 (如:http://localhost),出现It Works!网页. 查看状态: service ...
- datetimebox赋值或取值
datetimebox赋值或取值 $('#j_dateStart').datebox('setValue', ""); //赋予空值 $("#j_dateStart&qu ...
- vue-cli的项目加入骨架屏
在原生APP中我们经常可以看到,打开app时候,内容还没出来,app会被别的内容替代,这样很好的提升了用户体验.那么在webApp中,我们如何避免白屏的尴尬情况呢?可以通过 vue-skeleton- ...
- SQL删除重复数据(根据多个字段),pandas的nan存入数据库报错
delete from M_FACTOR_DATA_TEST a where (a.factor_id,a.data_date,a.stock_code) in (select factor_id,d ...
- Anaconda安装xgboost的过程和踩过的坑
win10下安装xgb,安装的过程波折起伏,花了5个小时,给后来人做参考喽 第一次尝试 利用以下两个软件 Git for Windows.MINGW进行安装. 安装可以参考:(https://blog ...
- 封装的一些常见的JS DOM操作和数据处理的函数.
//用 class 获取元素 function getElementsByClass(className,context) { context = context || document; if(do ...