虽然使用线程池ThreadPool让我们使用多线程变得容易,但是因为是由系统来分配的,如果想对线程做精细的控制就不太容易了,比如某个线程结束后执行一个回调方法。恰好Task可以实现这样的需求。这篇文章我从以下几点对Task进行总结。

  1. 认识Task
  2. 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/OtHXv6No674xlFX7HDU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u7/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的更多相关文章

  1. Java多线程系列--“JUC线程池”06之 Callable和Future

    概要 本章介绍线程池中的Callable和Future.Callable 和 Future 简介示例和源码分析(基于JDK1.7.0_40) 转载请注明出处:http://www.cnblogs.co ...

  2. Java多线程系列--“JUC线程池”01之 线程池架构

    概要 前面分别介绍了"Java多线程基础"."JUC原子类"和"JUC锁".本章介绍JUC的最后一部分的内容——线程池.内容包括:线程池架构 ...

  3. Java多线程系列--“JUC线程池”03之 线程池原理(二)

    概要 在前面一章"Java多线程系列--“JUC线程池”02之 线程池原理(一)"中介绍了线程池的数据结构,本章会通过分析线程池的源码,对线程池进行说明.内容包括:线程池示例参考代 ...

  4. c# 多线程系列二 自定义线程执行器

    看了第一篇文章,多线程系列,看到了在线程执行任务队列有了一定的了解~! 那么今天我来讲讲,怎么样构建通用的自定义线程概念! 线程执行任务,肯定要有目标,但是如果写死了,那么一个线程处理执行职能按照思路 ...

  5. .NET 4 并行(多核)编程系列之三 从Task的取消

    原文:.NET 4 并行(多核)编程系列之三 从Task的取消 .NET 4 并行(多核)编程系列之三 从Task的取消 前言:因为Task是.NET 4并行编程最为核心的一个类,也我们在是在并行编程 ...

  6. java多线程系列(七)---Callable、Future和FutureTask

    Callable.Future和FutureTask 前言:如有不正确的地方,还望指正. 目录 认识cpu.核心与线程 java多线程系列(一)之java多线程技能 java多线程系列(二)之对象变量 ...

  7. 多线程系列之 java多线程的个人理解(二)

    前言:上一篇多线程系列之 java多线程的个人理解(一) 讲到了线程.进程.多线程的基本概念,以及多线程在java中的基本实现方式,本篇主要接着上一篇继续讲述多线程在实际项目中的应用以及遇到的诸多问题 ...

  8. C# 多线程系列(四)

    Parallel类 Parallel类定义了for.foreach和invoke的静态方法.Parallel类使用多个任务,因此使用多个线程来完成这个作业. Parallel.For Parallel ...

  9. C# 多线程系列(三)

    线程池 创建线程需要时间,如果有不同的小任务要完成,就可以事先创建许多线程,在应完成这些任务时发出请求.这个线程数最好在需要更多线程时增加,在需要释放资源时减少. 不需要自己创建这样的一个列表.该列表 ...

随机推荐

  1. spring mvc 使用kaptcha配置生成验证码实例

    SpringMVC整合kaptcha(验证码功能) 一.依赖 <dependency> <groupId>com.github.penggle</groupId> ...

  2. linux系统坏了,进不了登陆界面怎么办?

    装oracle时,命令弄错了,结果系统崩溃之后就进不去系统了,启动后光标一直在闪烁,就是不显示登陆界面. 网上查了很多,什么grub引导之类的,完全没办法恢复系统. 系统坏了倒是无所谓,主要是系统上还 ...

  3. postgresql-递增uuid优点

    递增uuid的优点: https://blog.2ndquadrant.com/on-the-impact-of-full-page-writes/ 减小wal生成  

  4. 01-Linux的基本指令

    Linux的基本指令 基础指令(重点) 1.ls指令: 含义:ls(list) 用法1:#ls 含义:列出当前工作目录下的所有文件/文件夹的名称 用法2:#ls  路径 含义:列出指定路径下的所有文件 ...

  5. 3DMax——基础

    1.首次打开3DMAX设置单位: 自定义→单位设置→①系统单位设置→1单位=1.0毫米:②公制→毫米 注:室内单位为毫米,室外单位为米 2.从CAD导出可以导入到3DMAX的文件: 选中要导出的部分→ ...

  6. opencv2函数学习之erode、dilate:图像腐蚀和膨胀

    图像腐蚀和图像膨胀是图像中两种最基本形态学操作. ,-), ,int borderType=BORDER_CONSTANT, const Scalar& borderValue=morphol ...

  7. centos 7 hadoop的安装和使用

    准备工作 安装jdk 用户免密登录 安装参考文章: http://blog.csdn.net/circyo/article/details/46724335 http://www.linuxidc.c ...

  8. Maven启用代理服务器访问

    0.什么叫代理服务器? 代理服务器英文全称是(Proxy Server),其功能就是代理网络用户去取得网络信息.形象的说:它是网络信息的中转站. 代理服务器就好象一个大的Cache,这样就能显著提高浏 ...

  9. xshell 登陆堡垒机实现自动跳转

    1, 正常使用用户密码登录堡垒机并保存登陆配置 2, 配置登陆脚本 添加第一个: expect 为空send :ssh root@ip 添加第二个: expect root@ip's password ...

  10. Conditional特性用法

    说明:根据预处理标识符执行方法.Conditional 特性是 ConditionalAttribute 的别名,可应用于方法或属性类.相对于#if和#endif,更灵活更简洁和不易出错. 例如: # ...