C#任务线程池-仅在10个线程上运行100个任务 [英] C# Task thread pool - Running 100 tasks across only 10 threads

查看:1385
本文介绍了C#任务线程池-仅在10个线程上运行100个任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道是否有人可以向我指出有关异步/等待框架和线程池的正确方向?

I'm just wondering if someone can point me in the right direction about the async/await framework and thread pools?

基本上,我想做的是在一个单独的线程/异步中执行x个操作,但是最多跨y个线程.

Basically, what I'm trying to do is have x number of operations executed in a separate thread/async, but across a maximum of y threads.

例如,假设我有100个数据库操作: await _repository.WriteData(someData);

For example, let's say that I have 100 database operations: await _repository.WriteData(someData);

我想做的是有一些方法可以一次运行10个这样的操作(理想情况下,每个操作在一个单独的线程中,所以10个线程),当每个操作完成时,下一个启动然后可用的线程.然后,我们等待所有操作完成并且所有线程完成...

What I'd like to do is have some method of running 10 of those operations at a time (ideally in a separate thread each, so 10 threads), and as each one is finished, the next is kicked off on the thread that then becomes available. We then wait for all operations to complete and all threads to finish...

这是无需花费太多精力或增加大量复杂性即可轻易实现的东西吗?

Is this something that is readily achievable without too much effort or adding huge amounts of complexity?

推荐答案

我认为您将注意力集中在线程上,尤其是对于不需要线程执行的异步操作而言,是您错过了重点.

I think you're missing the point by focusing on threads, especially for asynchronous operations that don't require threads to execute.

.NET具有很好的ThreadPool,您可以使用.您不知道其中有多少个线程,也不在乎.它可以正常工作(直到它不起作用,并且您需要自己配置它,但这是非常先进的).

.NET has a great ThreadPool you can use. You don't know how many threads are in it and you don't care. It just works (until it doesn't and you need to configure it yourself, but that's very advance).

ThreadPool上运行任务非常简单.为每个操作创建一个任务,然后使用SemaphoreSlim限制它们,或使用现成的TPL Dataflow块.例如:

Running tasks on the ThreadPool is very simple. Either create a task for each operation and throttle them using a SemaphoreSlim or use the ready-made TPL Dataflow blocks. For example:

var block = new ActionBlock<SomeData>(
    _ => _repository.WriteDataAsync(_), // What to do on each item
    new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 10 }); // How many items at the same time

foreach (var item in items)
{
    block.Post(item); // Post all items to the block
}

block.Complete(); // Signal completion
await block.Completion; // Asynchronously wait for completion.

但是,如果您确实打算创建专用"线程,则可以将Task.Factory.StartNewLongRunning选项一起使用,该选项在ThreadPool之外创建专用线程.但是请记住,异步操作不需要在整个操作中维护相同的线程,因为异步操作不需要线程.因此,从专用线程开始可能没有意义(更多信息请参见我的博客:

If, however, you do plan on creating "dedicated" threads you can use Task.Factory.StartNew with the LongRunning option that creates a dedicated thread outside the ThreadPool. But keep in mind that async operations don't maintain the same thread throughout the operation as async operations don't need a thread. So starting on a dedicated thread may be pointless (more on that on my blog: LongRunning Is Useless For Task.Run With async-await)

这篇关于C#任务线程池-仅在10个线程上运行100个任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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