如何设置最大并行线程数

Curtis Chau
Curtis Chau
更新: 2026年7月21日

读取大量条形码时,依赖单线程进程可能会造成性能瓶颈并限制可扩展性。 然而,使用并行线程可以让您的应用程序同时处理多张图片,有效地倍增总处理能力并缩短完成批处理的时间。

为这些线程设置最大限制是优化性能的有效方法。 它通过在处理器核心之间平衡工作负载,确保应用程序充分利用硬件的潜力。 这种方法提高了效率,使应用程序运行更流畅,同时提供更快的结果。

IronBarcode 提供了一种简单的方法来控制此限制,从而确保实现最佳的机器性能。 以下部分演示如何轻松设置这些线程限制。



设置最大并行线程数

在这个例子中,我们将使用大量的条形码图像来说明使用多线程进程而不是单线程进程的可扩展性和效率。 您可以在这里下载图片文件夹。

要配置IronBarcode使用多个线程,首先实例化一个新的BarcodeReaderOptions对象,并将Multithreaded设置为true。 之后,通过分配一个整数值来设置MaxParallelThreads属性。 默认情况下,MaxParallelThreads被设置为4。

配置完成后,从文件夹中导入大量条形码图像。 然后,使用循环,通过Read方法读取条形码图像目录,传递文件路径和配置好的BarcodeReaderOptions。 最后,通过访问BarcodeResults显示条形码的值和类型。

using Google.Protobuf.WellKnownTypes;
using IronBarCode;
using System;
using System.IO;

int maxParallelThreads = 4;


var optionsFaster = new BarcodeReaderOptions
{
    // Set Max threads to 4
    Multithreaded = true,
    MaxParallelThreads = maxParallelThreads,
};

// Dynamically get the "images" folder in the current directory
string folderPath = Path.Combine(Directory.GetCurrentDirectory(), "images");

// Retrieve all JPG files in the directory
var pdfFiles = Directory.GetFiles(folderPath, "*.jpg");

foreach (var file in pdfFiles)
{
    // Read the barcode
    var results = BarcodeReader.Read(file, optionsFaster);

    foreach (var result in results)
    {
        // Show the type and value for every barcode found
        Console.WriteLine($"Value: {result.Value}, Type: {result.BarcodeType}");

    }

}

输出

多线程输出

如控制台输出所示,它会显示每个对应图像的条形码值和类型。

设置合适的最大并行线程数

当Multithreaded属性设置为true时,MaxParallelThreads属性默认是4。虽然赋给MaxParallelThreads的整数没有严格限制,但设置的值高于硬件的逻辑核心能力实际上会导致性能下降。 这是因为处理器无法处理过多的上下文切换,这可能会导致性能开销而不是速度提升。 因此,MaxParallelThreads的正确值取决于计算机的规格,开发者应测试以找到他们环境的最佳值。

在这个例子中,我们将展示与上面相同的多线程场景,但加入计时器以比较默认值4与使用Environment.ProcessorCount来利用所有可用线程。 MaxParallelThreads 在我们的案例中,我们使用的是一台有32个逻辑处理器的电脑,因此Environment.ProcessorCount将被设置为32。

using IronBarCode;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;

// Set the max parallel threads to the number of processor cores
int maxParallelThreads = Environment.ProcessorCount;


var optionsFaster = new BarcodeReaderOptions
{
    // Set Max threads to the number of processor cores
    Multithreaded = true,
    MaxParallelThreads = maxParallelThreads,
    ExpectMultipleBarcodes = true,
};

// Start timing the process
var stopwatch = Stopwatch.StartNew();
// Dynamically get the "images" folder in the current directory
string folderPath = Path.Combine(Directory.GetCurrentDirectory(), "images");

// Check if directory exists to prevent crashes
if (!Directory.Exists(folderPath))
{
    Console.WriteLine($"Error: The directory '{folderPath}' does not exist.");
    return;
}

// Get all JPG files in the directory
var pdfFiles = Directory.GetFiles(folderPath, "*.jpg");

foreach (var file in pdfFiles)
{
    // Read the barcode
    var results = BarcodeReader.Read(file, optionsFaster);

    if (results.Any())
    {
        Console.WriteLine($"Barcode(s) found in: {Path.GetFileName(file)}");
        foreach (var result in results)
        {
            Console.WriteLine($"  Value: {result.Value}, Type: {result.BarcodeType}");

        }
    }
}

stopwatch.Stop();

// Print number of images the barcode reader could decode
Console.WriteLine($" Max parallel threads of {maxParallelThreads} with {stopwatch.Elapsed.TotalSeconds:F2}s");

输出

4线程处理时间

4 处理器

在上述描述的32核机器上运行的这个例子中,此过程的处理时间大约为84秒。 实际计时因硬件和工作负载而异。

处理时间与环境处理器数量

32 处理器

如您所见,在这个例子中,该操作的处理时间大约为53秒,比仅用四个线程运行时更快。 但是请注意,使用更多线程并不能保证性能提升,因为性能取决于主机处理器。 一般经验法则是使用可用处理器的最大数量减一,以确保仍然有一个线程可用于其他系统操作。

警告: 必须配置项目环境以允许多线程。 否则,将Multithreaded设置为true并增加MaxParallelThreads不会提高处理速度,甚至可能降低速度。

常见问题

设置最大并行线程如何优化条码生成性能?

通过利用所有可用的处理器核心,设置最大并行线程数可以优化性能,允许同时处理多个条码图像。这增加了处理能力并减少了批处理作业所需的时间。

如何使用IronBarcode在C#中设置最大并行线程?

要在IronBarcode中设置最大并行线程,需实例化一个BarcodeReaderOptions对象,将Multithreaded设为true,并为MaxParallelThreads分配一个整数值。此设置将确定执行条码处理的线程数量。

IronBarcode中最大并行线程数的默认值是多少?

IronBarcode中MaxParallelThreads的默认值设置为4。此初始配置旨在确保大多数多核系统上高效的处理。

MaxParallelThreads的值能超过处理器核心数吗?

虽然没有硬性限制,但将MaxParallelThreads设置为大于可用逻辑核心的值可能会因过多的上下文切换和开销而降低性能。

设置MaxParallelThreads值时应考虑什么?

在设置MaxParallelThreads时应考虑硬件中的逻辑处理器数量。最佳值允许高效的多线程处理而不使处理器负担过重,通常使用核心数减一以获得最佳性能。

使用Environment.ProcessorCount设置MaxParallelThreads有什么效果?

使用Environment.ProcessorCount将MaxParallelThreads设置为逻辑处理器的总数,充分利用可用的线程资源,大大提高多核机器上的处理时间。

硬件容量对多线程条码处理有什么影响?

硬件容量直接影响多线程的有效性。逻辑核心的数量决定了可行的线程数量,平衡工作负载并避免因资源限制导致的性能瓶颈。

为何增加MaxParallelThreads不一定总能提高性能?

增加MaxParallelThreads可能会使CPU过载,特别是如果它超过核心数量,导致收益递减,甚至导致性能下降而不是提升。

如果示例代码中指定的图像目录不存在会发生什么?

如果指定的目录不存在,代码中包含一个检查机制,输出错误消息并退出进程,以防止在条码读取过程中出现崩溃。

确定最佳MaxParallelThreads值的推荐方法是什么?

建议在特定环境中测试不同的配置,考虑系统资源和工作负载类型,以确定用于最大化性能的最佳MaxParallelThreads值。

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

阅读更多

准备开始了吗?

NuGet 下载量 2,575,633版本:2026.11刚刚发布