使用 Tasks (TPL) 库会使应用程序多线程吗? [英] Does using Tasks (TPL) library make an application multithreaded?

查看:16
本文介绍了使用 Tasks (TPL) 库会使应用程序多线程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近面试的时候,被问到这个问题.

Recently when being interviewed, I got this question.

问:你写过多线程应用程序吗?

Q: Have you written multithreaded applications?

答:是的

问:需要解释更多吗?

A:我使用Tasks(任务并行库)来执行一些任务,比如在加载UI时等待来自互联网的一些信息.这提高了我的应用程序可用性.

A: I used Tasks (Task Parallel library) to carry out some tasks like waiting for some info from internet while loading UI. This improves my application usability.

问:但是,仅仅使用了TPL 就意味着您编写了一个多线程 应用程序?

Q: But, just you have used TPL means that you have written an multithreaded application?

我:(不知道该说什么1)

Me: (Not sure what to say1)

那么,到底什么是多线程应用程序?和使用Tasks有什么不同吗?

So, whats exactly a multi-threaded application? Is it different from using Tasks?

推荐答案

任务可用于表示发生在多个线程上的操作,但它们没有到.一个可以编写复杂的 TPL 应用程序,这些应用程序只能在单个线程中执行.例如,当您有一个任务代表对某些数据的网络请求时,该任务不会创建额外的线程来实现该目标.这样的程序(希望)是异步的,但不一定是多线程的.

Tasks can be used to represent operations taking place on multiple threads, but they don't have to. One can write complex TPL applications that only ever execute in a single thread. When you have a task that, for example, represents a network request for some data, that task is not going to create additional threads to accomplish that goal. Such a program is (hopefully) asynchronous, but not necessarily mutlithreaded.

并行是同时做不止一件事.这可能是也可能不是多线程的结果.

Parallelism is doing more than one thing at the same time. This may or may not be the result of multiple threads.

让我们在这里打个比方.

Let's go with an analogy here.

这是鲍勃做饭的方式:

  1. 他装满一壶水,煮沸.
  2. 然后他将意大利面放入水中.
  3. 面食完成后,他将其沥干.
  4. 他准备酱汁的配料.
  5. 他将酱汁的所有原料放入平底锅中.
  6. 他煮酱汁.
  7. 他把酱汁抹在意大利面上.
  8. 他吃晚饭.

Bob 在做饭时完全同步做饭,没有多线程、异步或并行.

Bob has cooked entirely synchronously with no multithreading, asynchrony, or parallelism when cooking his dinner.

这是简做晚饭的方式:

  1. 她装满一壶水并开始煮沸.
  2. 她准备酱汁的原料.
  3. 她将意大利面放入沸水中.
  4. 她将食材放入平底锅中.
  5. 她沥干她的意大利面.
  6. 她把酱汁放在她的意大利面上.
  7. 她吃晚饭.

Jane 在做饭时利用异步烹饪(没有任何多线程)来实现并行性.

Jane leveraged asynchronous cooking (without any multithreading) to achieve parallelism when cooking her dinner.

这是Servy 做晚餐的方式:

Here is how Servy cooks dinner:

  1. 他让鲍勃煮一壶水,煮好后放入意大利面,然后端上意大利面.
  2. 他让简准备酱汁的原料,煮熟,完成后放在意大利面上.
  3. 他等待鲍勃和简完成.
  4. 他吃晚饭.

Servy 利用多个线程(工作线程),每个线程都单独同步工作,但彼此之间异步工作以实现并行性.

Servy leveraged multiple threads (workers) who each individually did their work synchronously, but who worked asynchronously with respect to each other to achieve parallelism.

当然,如果我们考虑例如我们的炉子是有两个燃烧器还是只有一个燃烧器,这会变得更加有趣.如果我们的炉子有两个燃烧器,那么我们的两个线程 Bob 和 Jane 都可以在不妨碍彼此的情况下完成他们的工作.他们可能会撞到肩膀,或者每个人都时不时地尝试从同一个柜子里拿东西,所以他们每个人都会慢一点一点,但不会太多.如果他们每个人都需要共用一个炉灶,那么当另一个人正在工作时,他们实际上根本无法完成很多工作.在这种情况下,工作实际上不会比让一个人完全同步做饭更快,就像鲍勃独自一人时所做的那样.在这种情况下,我们使用多个线程进行烹饪,但我们的烹饪不是并行化.并非所有多线程工作实际上都是并行工作.当您在具有一个 CPU 的机器上运行多个线程时会发生这种情况.您实际上并没有比仅使用一个线程更快地完成工作,因为每个线程只是轮流工作.(这并不意味着多线程程序在单核 CPU 上毫无意义,它们不是,只是使用它们的原因不是为了提高速度.)

Of course, this becomes all the more interesting if we consider, for example, whether our stove has two burners or just one. If our stove has two burners then our two threads, Bob and Jane, are both able to do their work without getting in each others way, much. They might bump shoulders a bit, or each try to grab something from the same cabinet every now and then, so they'll each be slowed down a bit, but not much. If they each need to share a single stove burner though then they won't actually be able to get much done at all whenever the other person is doing work. In that case, the work won't actually get done any faster than just having one person doing the cooking entirely synchronously, like Bob does when he's on his own. In this case we are cooking with multiple threads, but our cooking isn't parallelized. Not all multithreaded work is actually parallel work. This is what happens when you are running multiple threads on a machine with one CPU. You don't actually get work done any faster than just using one thread, because each thread is just taking turns doing work. (That doesn't mean multithreaded programs are pointless on one cores CPUs, they're not, it's just that the reason for using them isn't to improve speed.)

我们甚至可以考虑这些厨师将如何使用任务并行库来完成他们的工作,以了解 TPL 的哪些用途对应于这些厨师类型中的每一种:

We can even consider how these cooks would do their work using the Task Parallel Library, to see what uses of the TPL correspond to each of these types of cooks:

所以首先我们有 bob,只需编写普通的非 TPL 代码并同步执行所有操作:

So first we have bob, just writing normal non-TPL code and doing everything synchronously:

public class Bob : ICook
{
    public IMeal Cook()
    {
        Pasta pasta = PastaCookingOperations.MakePasta();
        Sauce sauce = PastaCookingOperations.MakeSauce();
        return PastaCookingOperations.Combine(pasta, sauce);
    }
}

然后是 Jane,她启动了两个不同的异步操作,然后在启动每个异步操作后等待这两个操作来计算她的结果.

Then we have Jane, who starts two different asynchronous operations, then waits for both of them after starting each of them to compute her result.

public class Jane : ICook
{
    public IMeal Cook()
    {
        Task<Pasta> pastaTask = PastaCookingOperations.MakePastaAsync();
        Task<Sauce> sauceTask = PastaCookingOperations.MakeSauceAsync();
        return PastaCookingOperations.Combine(pastaTask.Result, sauceTask.Result);
    }
}

在此提醒一下,Jane 使用的是 TPL,而且她的大部分工作都是并行完成的,但她只使用一个线程来完成她的工作.

As a reminder here, Jane is using the TPL, and she's doing much of her work in parallel, but she's only using a single thread to do her work.

然后我们有 Servy,它使用 Task.Run 创建一个任务,表示在另一个线程中工作.他启动了两个不同的 worker,让它们每个都同步做一些工作,然后等待两个 worker 完成.

Then we have Servy, who uses Task.Run to create a task that represents doing work in another thread. He starts two different workers, has them each both synchronously do some work, and then waits for both workers to finish.

public class Servy : ICook
{
    public IMeal Cook()
    {
        var bobsWork = Task.Run(() => PastaCookingOperations.MakePasta());
        var janesWork = Task.Run(() => PastaCookingOperations.MakeSauce());
        return PastaCookingOperations.Combine(bobsWork.Result, janesWork.Result);
    }
}

这篇关于使用 Tasks (TPL) 库会使应用程序多线程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆