The Task Parallel Library (TPL) is a set of public types and APIs in the System.Threading and System.Threading.Tasks namespaces. The purpose of the TPL is to make developers more productive by simplifying the process of adding parallelism and concurrency to applications.

上述为MSDN中关于TPL库的描述。通过以上描述可知,TPL的主要目的是通过简化应用中雨并行/并发相关逻辑的处理来提高软件开发人员的效率。

这里主要套路TPL库中有关Task和Parallel的用法。

1、构建Task数组

Task[] taskArray = new Task[];

2、初始化数组成员(此处使用Factory)

taskArray[] = Task.Factory.StartNew();
taskArray[] = Task.Factory.StartNew();
taskArray[] = Task.Factory.StartNew();

3、使用Parallel构建处理逻辑

Parallel.For(, ,
(i => Console.WriteLine(string.Format("print {0}.", i))));
Parallel.ForEach(items,
(i => Console.WriteLine(string.Format("print {0}.", i))));

4、等待所有任务完成

Task.WaitAll(taskArray);

TPL(Task Parallel Library)多线程、并发功能的更多相关文章

  1. Using the Task Parallel Library (TPL) for Events

    Using the Task Parallel Library (TPL) for Events The parallel tasks library was introduced with the ...

  2. C#5.0之后推荐使用TPL(Task Parallel Libray 任务并行库) 和PLINQ(Parallel LINQ, 并行Linq). 其次是TAP(Task-based Asynchronous Pattern, 基于任务的异步模式)

    学习书籍: <C#本质论> 1--C#5.0之后推荐使用TPL(Task Parallel Libray 任务并行库) 和PLINQ(Parallel LINQ, 并行Linq). 其次是 ...

  3. Winform Global exception and task parallel library exception;

    static class Program { /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static vo ...

  4. Task Parallel Library01,基本用法

    我们知道,每个应用程序就是一个进程,一个进程有多个线程.Task Parallel Library为我们的异步编程.多线程编程提供了强有力的支持,它允许一个主线程运行的同时,另外的一些线程或Task也 ...

  5. 用读写锁三句代码解决多线程并发写入文件 z

    C#使用读写锁三句代码简单解决多线程并发写入文件时提示“文件正在由另一进程使用,因此该进程无法访问此文件”的问题 在开发程序的过程中,难免少不了写入错误日志这个关键功能.实现这个功能,可以选择使用第三 ...

  6. 多线程并发执行任务,取结果归集。终极总结:Future、FutureTask、CompletionService、CompletableFuture

    目录 1.Futrue 2.FutureTask 3.CompletionService 4.CompletableFuture 5.总结 ================正文分割线========= ...

  7. C# 防止同时调用=========使用读写锁三行代码简单解决多线程并发的问题

    http://www.jb51.net/article/99718.htm     本文主要介绍了C#使用读写锁三行代码简单解决多线程并发写入文件时提示"文件正在由另一进程使用,因此该进程无 ...

  8. Java多线程并发编程一览笔录

    线程是什么? 线程是进程中独立运行的子任务. 创建线程的方式 方式一:将类声明为 Thread 的子类.该子类应重写 Thread 类的 run 方法 方式二:声明实现 Runnable 接口的类.该 ...

  9. Java 多线程并发编程面试笔录一览

    知识体系图: 1.线程是什么? 线程是进程中独立运行的子任务. 2.创建线程的方式 方式一:将类声明为 Thread 的子类.该子类应重写 Thread 类的 run 方法 方式二:声明实现 Runn ...

随机推荐

  1. HDU 1285

    #include<stdio.h> #include<string.h> int degree[505],vis[505],map[501][501]; int main() ...

  2. 尝试加载 Oracle 客户端库时引发 BadImageFormatException。如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将出现此问题。

    从10G开始,Oracle提供了一个较为轻量级的客户包,叫做Instant Client. 将它安装好后,就不用再安装庞大的Oracle Client了. 这样一来,只要客户端下载Instant Cl ...

  3. Spark + Mesos 注意事项

    在使用spark-submit的过程中,需要注意 spark-defaults.conf Spark-defaults.conf的作用范围要搞清楚,编辑driver所在机器上的spark-defaul ...

  4. 【笔试】T实习生2014 总结

    2014.4.12 是T的广州站,就在本校,很方便.考前一两天,临时抱佛脚,看着HTMLdog上的网页知识.就算考前,还在懊悔自己为什么不好好利用清明假期,多看点,看多点?哎,哎.. 谁知道一拿到试卷 ...

  5. 【HTML】Beginner3:ParagraphsEmphasisLine breaks

    1.Paragraphs </p>     distinct blocks of the text     Think of the HTML contents as if it were ...

  6. bzoj 3196 Tyvj 1730 二逼平衡树(线段树套名次树)

    3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1807  Solved: 772[Submit][Stat ...

  7. image sensor 积分时间

    integration  time 积分时间 当光源周期与integration time成整数倍时才不会产生flicker integration time即积分时间是以行为单位表示曝光时间(exp ...

  8. redis ltrim命令

    LTRIM key start stop 相关命令 BLPOP BRPOP BRPOPLPUSH LINDEX LINSERT LLEN LPOP LPUSH LPUSHX LRANGE LREM L ...

  9. HW3.16

    public class Solution { public static void main(String[] args) { int randomValue = (int)(Math.random ...

  10. @ContextConfiguration注解说明

    @ContextConfiguration Spring整合JUnit4测试时,使用注解引入多个配置文件单个文件 @ContextConfiguration(Locations="../ap ...