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线程池 管理线程开销最好的方式: 尽量少的创建线程并且能将线程反复利用(线程池初始化时没有线程,有程序请求线程则创建线程): 最好不要销毁而是挂起线程达到避免性能损失(线程池创建的线程完成任 ...
随机推荐
- 一个用pyton写的监控服务端进程的软件hcm
使用udp实现,简单,方便,不用三次握手 1. 所有部署服务器进程的机器有一个代理进程hagent,用来监听hcm console中发送过来的命令 2.hcm需要提供以下命令 start :普通方式启 ...
- windows多线程应用编程注意事项
1,资源争用保护 对于文件操作.界面资源.GDI操作等一般由主线程完成的任务,要加以顺序化处理(serialization),即一个资源一次只能由一个线程访问,多个线程同时访问将导致错误. 方法一般可 ...
- 继续C#开发or转做产品
本人今年大四,C#开发,在一家公司实习了一年后,面临一个选择,是继续C#开发还是转做产品?我C#开发能力目前还一般,但很有兴趣.也喜欢设计与创意,做过产品专员.求大婶们指导!
- golang协程——通道channel阻塞
新的一年开始了,不管今天以前发生了什么,向前看,就够了. 说到channel,就一定要说一说线程了.任何实际项目,无论大小,并发是必然存在的.并发的存在,就涉及到线程通信.在当下的开发语言中,线程通讯 ...
- Codeforces_766_D_(并查集)
D. Mahmoud and a Dictionary time limit per test 4 seconds memory limit per test 256 megabytes input ...
- MFC获取各窗口指针句柄
MFC在很多的对话框操作中,我们经常要用到在一个对话框中调用另一个对话框的函数或变量.可以用如下方法来解决. HWND hWnd=::FindWindow(NULL,_T("Sphere ...
- 文件内容差异对比-python
上次没精力时候,看了下python自动化运维,给print加了颜色,新鲜哒 今天来写写文件对比 step1:引入difflib库(无需安装,python自带) step2:将文件内容按行分割,spli ...
- 安装charles
简介: Charles主要的功能包括: 支持SSL代理.可以截取分析SSL的请求. 支持流量控制.可以模拟慢速网络以及等待时间(latency)较长的请求. 支持AJAX调试.可以自动将json或xm ...
- python面试题之如何在Python中创建自己的包
Python中创建包是比较方便的,只需要在当前目录建立一个文件夹, 文件夹中包含一个__init__.py文件和若干个模块文件, 其中__init__.py可以是一个空文件,但还是建议将包中所有需要导 ...
- 解决Python打包exe控制台无法粘贴问题
使用pyinstaller打包生成可执行exe文件后,发现启用input()接受键盘输入时窗口无法粘贴也无法右键,找了好久终于找到问题所在: 一是通过右键单击控制台主题边框在弹出的菜单中选择编辑.粘贴 ...