让用户选择他希望程序使用多少线程 [英] Make user choosing how many threads he want the program to use

查看:32
本文介绍了让用户选择他希望程序使用多少线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让一个读取 .txt 文件每一行的程序对每一行做一些事情.

I'd like to make a program that read each line of a .txt file do something with each line.

我希望用户能够选择线程,例如,如果他选择 10 个线程,那么程序将能够同时在 10 行上执行某些操作.

I would like to user to be able to choose threads so if he choose for example 10 threads so the program will be able to do something on 10 lines at the same time.

假设文本文件包含例如 1 000 000 行,我该怎么办?

Let say the text file contains for example 1 000 000 lines, how must I do ?

这就是我逐行阅读的实际内容:

This is what I actually have for reading the lines one by one :

var lines = File.ReadLines(accounts);
foreach (var line in lines)
{
    // Start a thread, but maximum 10 threads at the same if the user input is 10, for example.
}

非常感谢.

推荐答案

您不能绝对控制线程数.使用的数量取决于可用数量和其他因素.但是您可以通过指定要使用的最大线程数来限制线程数.

You can't absolutely control the number of threads. The number used depends on the number available and other factors. But you can limit the number of threads by specifying the maximum number to use.

使用 Parallel.ForEach:

var options = new ParallelOptions {MaxDegreeOfParallelism = 5};
Parallel.ForEach(lines, options, line =>
{
    // do something with each line
});

不过,

MaxDegreeOfParellelism 并不能说明全部情况.它仅限制线程数.使用的线程来自托管线程池.可以使用 ThreadPool.SetMinThreadsThreadPool.SetMaxThreads 设置可用于启动新任务的线程数.

MaxDegreeOfParellelism doesn't tell the whole story, though. It merely limits the number of threads. The threads used come from the managed thread pool. The number of threads available for starting new tasks can be set using ThreadPool.SetMinThreads and ThreadPool.SetMaxThreads.

但这仍然不能说明全部情况.可以执行的并发操作数受 CPU 内核数的限制.它还取决于这些操作的作用.例如,无论您运行了多少个线程,它们都不能同时写入磁盘.增加线程数超过某个点可能会降低性能.

But that still doesn't tell the whole story. The number of concurrent operations that can be performed is limited by your number of CPU cores. It also depends on what those operations do. For example, no matter how many threads you have running they can't all write to the disk at the same time. Increasing the number of threads beyond a certain point may decrease performance.

因此,虽然试验很有趣,但在大多数情况下,程序的用户不太可能知道应该同时运行多少线程.

So while it's interesting to experiment with, in most scenarios it's very unlikely that the user of a program would know how many threads should run concurrently.

而这只是实现多线程的一种方式.Parellel.ForEach 在您拥有 IEnumerable(如从文件中读取的行数组)并希望并行执行它们时很方便.

And this is only one way to accomplish multithreading. Parellel.ForEach is a convenience for when you have an IEnumerable (like the array of lines read from the file) and want to execute them in parallel.

另一个需要考虑的因素是,当您并行执行操作时,您无法保证它们的执行顺序.它们可能看起来运行 FIFO,但随后您再次运行它,它们不会.

One more factor to take into account is that when you perform operations in parallel you can't guarantee the sequence in which they will execute. They may appear to run FIFO, but then you'll run it again and they won't.

这篇关于让用户选择他希望程序使用多少线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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