多线程系列(3)任务Task
虽然使用线程池ThreadPool让我们使用多线程变得容易,但是因为是由系统来分配的,如果想对线程做精细的控制就不太容易了,比如某个线程结束后执行一个回调方法。恰好Task可以实现这样的需求。这篇文章我从以下几点对Task进行总结。
- 认识Task
- Task的用法
认识Task
Task类在命名空间System.Threading.Tasks下,通过Task的Factory返回TaskFactory类,以TaskFactory.StartNew(Action)方法可以创建一个新的异步线程,所创建的线程默认为后台线程,不会影响前台UI窗口的运行。
如果要取消线程,可以利用CancellationTakenSource对象。如果要在取消任务后执行一个回调方法,则可以使用Task的()方法。
Task的用法
利用Task对之前的例子进行重写和扩展。代码如下。
namespace ThreadDemo
{
class Program
{
static void Main(string[] args)
{
// 创建CancellationTokenSource对象用于取消Task
CancellationTokenSource cancelTokenSource = new CancellationTokenSource(); Fish fish1 = new Fish() { Name = "小黄鱼", Score = };
Fish fish2 = new Fish() { Name = "大鲨鱼", Score = }; // 创建一个Task
Task task1 = new Task(() => fish1.Move(cancelTokenSource.Token), cancelTokenSource.Token);
task1.Start(); // Task1被取消后的回调方法(小黄鱼被击中后显示积分)
task1.ContinueWith(fish1.ShowScore); Task task2 = new Task(() => fish2.Move(cancelTokenSource.Token), cancelTokenSource.Token);
task2.Start();
task2.ContinueWith(fish2.ShowScore); // 按任意键发射
Console.ReadKey(); // 武器工厂线程池
Gun gun = new Gun();
LaserGun laserGun = new LaserGun();
TaskFactory taskFactory = new TaskFactory();
Task[] tasks = new Task[]
{
taskFactory.StartNew(()=>gun.Fire()),
taskFactory.StartNew(()=>laserGun.Fire())
}; // 执行武器开火
taskFactory.ContinueWhenAll(tasks, (Task) => { }); cancelTokenSource.Cancel();
Console.ReadKey();
}
} /// <summary>
/// 鱼
/// </summary>
public class Fish
{
public string Name { get; set; } public int Score { get; set; } public Fish()
{
} public void Move()
{
Console.WriteLine(string.Format("{0}在游来游去...", Name));
} /// <summary>
/// 游动
/// </summary>
/// <param name="cancelToken"></param>
public void Move(CancellationToken cancelToken)
{
while (!cancelToken.IsCancellationRequested)
{
Console.WriteLine(string.Format("{0}在游来游去...", Name)); // 阻塞1秒
Thread.Sleep();
}
} /// <summary>
/// 中枪后显示奖励
/// </summary>
/// <param name="task"></param>
public void ShowScore(Task task)
{
Console.WriteLine(string.Format("{0}中弹了,你得到{1}分",Name,Score));
}
}
}
程序运行结果如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbkAAAFeCAIAAACXd/ZFAAAgAElEQVR4nO2cd7weRdXHn3/F8tp97YqoiFIURVBKROkgvYUiVTohCSUJIdxAIJ2EhCQQSkJC6L1JSXQRlK5GY+8tiAoGCCkQ3n3/GBzm7sw5c2Z2Zve59/6+n/3cz+7smTOz+9z93rP7PM/tlAAAAFh6eno6y37yGBYsWLBgYRa4EgsWLFj8yxuuXG+DE+ssb/7kSW/+5Elv+dRJb/nUyW/59Mlv/fTJb93wlLdteMrbPnPq/6hloyFv32jI2z875B2fPe0dnzvtnZ877Z0bD33nxkPftcmwd20y7N2bDHv3psPfs+nw92w2/D2bnf7ez5/+3s+f8b4vnPG+L5zxv5ufqZb3f/Gs93/xrA986awPfGnEB7cY8cEtRn7wyyM/9OWRH9py1Ie3HPXhLUd9ZKuzP7LV2R/5yuiPfmX0R786+mNfPedjW5/z8a3P+fg2Yz6+zZj1txmz/rbnfmLbcz+xXc8G2/VsMKhng0FjP/m1sZ/82thPbX/ep7Y/79NfP18tG35j3IbfGPeZHcZ9ZocLNtrxgo12vGCjnS787E4Xfnbn8Z/befzndh6/8S4TNt5lwsa7Tthk14mb7DZx090mbbr7pM12n7TZHpM322Py5/eY/PlvTvnCN6d8Yc8pm+85dfO9pm6+10Vf3PuiL+590Zf2mfalfaZtsc+0LfadvsW+07+838Vf3u/iLfe/eMv9Z2x1wIytDpix1YEzv3LgzK8cOPOrB13y1YMu2frgWVsfPGvrwbO2GTx7m0Nmb3vI7G0PnbPdoXO2O+zS7Q67dNBhlw46/LKvHX7Z17512fbfmrv9EXO3P2Lu14+8/OtHXv6No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lyJpd8vqCu7/KdenLJaFu7Ksix7enrM/H5X6rfb+d8eLFj68YK6sjt/mitq8fpKsihRhrnS/GgSNTZciaXfL6gru/ynXihlyV1pitLMj7oSCxb/grqyO3+aK2qpWVeWVkUZ4MpleF6JZcAvqCu7/Kde6jyvrIgy+HmlngH/24MFSz9eUFd2509zRS3R74Pboox0JTM2XIml3y+oK7v8p/wVdC6UKM38cCUWLP4FdWV3/jRXJK+gcxclyspAcCUWLP4FdWWX/5S/gpWFEaX/HrxHTGg8AH0X1JVd+DPCV3JR9vCuBABQtK4G/Kz8jHgFg0RJunKZoJTFggULloGzOFxZAgAAsKi6EgsWLFiwUAtciQULFiz+paenpxP9ziAAAAwcOhneMAQAgP7G/wNylSkbwuIG5QAAAABJRU5ErkJggg==" alt="" />
下一篇文章我将要总结关于多线程的安全的问题,欢迎大家继续关注。
多线程系列(3)任务Task的更多相关文章
- Java多线程系列--“JUC线程池”06之 Callable和Future
概要 本章介绍线程池中的Callable和Future.Callable 和 Future 简介示例和源码分析(基于JDK1.7.0_40) 转载请注明出处:http://www.cnblogs.co ...
- Java多线程系列--“JUC线程池”01之 线程池架构
概要 前面分别介绍了"Java多线程基础"."JUC原子类"和"JUC锁".本章介绍JUC的最后一部分的内容——线程池.内容包括:线程池架构 ...
- Java多线程系列--“JUC线程池”03之 线程池原理(二)
概要 在前面一章"Java多线程系列--“JUC线程池”02之 线程池原理(一)"中介绍了线程池的数据结构,本章会通过分析线程池的源码,对线程池进行说明.内容包括:线程池示例参考代 ...
- c# 多线程系列二 自定义线程执行器
看了第一篇文章,多线程系列,看到了在线程执行任务队列有了一定的了解~! 那么今天我来讲讲,怎么样构建通用的自定义线程概念! 线程执行任务,肯定要有目标,但是如果写死了,那么一个线程处理执行职能按照思路 ...
- .NET 4 并行(多核)编程系列之三 从Task的取消
原文:.NET 4 并行(多核)编程系列之三 从Task的取消 .NET 4 并行(多核)编程系列之三 从Task的取消 前言:因为Task是.NET 4并行编程最为核心的一个类,也我们在是在并行编程 ...
- java多线程系列(七)---Callable、Future和FutureTask
Callable.Future和FutureTask 前言:如有不正确的地方,还望指正. 目录 认识cpu.核心与线程 java多线程系列(一)之java多线程技能 java多线程系列(二)之对象变量 ...
- 多线程系列之 java多线程的个人理解(二)
前言:上一篇多线程系列之 java多线程的个人理解(一) 讲到了线程.进程.多线程的基本概念,以及多线程在java中的基本实现方式,本篇主要接着上一篇继续讲述多线程在实际项目中的应用以及遇到的诸多问题 ...
- C# 多线程系列(四)
Parallel类 Parallel类定义了for.foreach和invoke的静态方法.Parallel类使用多个任务,因此使用多个线程来完成这个作业. Parallel.For Parallel ...
- C# 多线程系列(三)
线程池 创建线程需要时间,如果有不同的小任务要完成,就可以事先创建许多线程,在应完成这些任务时发出请求.这个线程数最好在需要更多线程时增加,在需要释放资源时减少. 不需要自己创建这样的一个列表.该列表 ...
随机推荐
- 【转】JS浮点数运算Bug的解决办法
37.5*5.5=206.08 (JS算出来是这样的一个结果,我四舍五入取两位小数) 我先怀疑是四舍五入的问题,就直接用JS算了一个结果为:206.08499999999998 怎么会这样,两个只有一 ...
- docker部署PiggyMetrics分布式微服务
在上一篇文章里http://www.cnblogs.com/lyhero11/p/8686058.html, 讲解了如何在windows10下安装docker社区版. 那如何利用docker落地一个分 ...
- Selenium3 + Python3自动化测试系列五——常用断言Assertion
断言Assertion 验证应用程序的状态是否同所期望的一致. 常见的断言包括:验证页面内容,如标题是否为X或当前位置是否正确,或是验证该复选框是否被勾选. selenium 提供了三种模式的断言:a ...
- Dev Label显示不同颜色字体
labelControl1.Text = "<size=14>Size = 14<br>" + "<b>Bold</b> ...
- 使用代码查看Nutch爬取的网站后生成的SequenceFile信息
必须针对data文件中的value类型来使用对应的类来查看(把这个data文件,放到了本地Windows的D盘根目录下). 代码: package cn.summerchill.nutch; impo ...
- C# 多线程六之Task(任务)二
前面介绍了Task的由来,以及简单的使用,包括开启任务,处理任务的超时.异常.取消.以及如果获取任务的返回值,在回去返回值之后,立即唤起新的线程处理返回值.且如果前面的任务发生异常,唤起任务如果有效的 ...
- Fragment中启动一个新的Activity
最近遇到一个小问题,就是我在主界面中用的是Fragment,其中四个Fragment,然后打算在其中一个里边,写一个TextView(准确地说是Linearout)的单击事件,然后跳转到另外一个Act ...
- c# winform as3相互调用
C#主要代码: 首先要添加COM组件-Shockwave Flash Object //接收flash发送过来的信息 private void axShockwaveFlash1_Fla ...
- 31-hadoop-hbase-mapreduce操作hbase
有一些大的文件,需要存入HBase中,其思想是先把文件传到HDFS上,利用map阶段读取<key,value>对,可在reduce把这些键值对上传到HBase中. HbaseMapper: ...
- 《Netty权威指南》(三)Netty 入门应用
Netty 服务端 Netty 客户端