最大並列スレッドの設定方法

This article was translated from English: Does it need improvement?
Translated
View the article in English

大量の BarCode を読み取る場合、シングルスレッド処理に頼るとパフォーマンスのボトルネックが発生し、スケーラビリティが制限される可能性があります。 しかし、並列スレッドを使用することで、アプリケーションは複数の画像を同時に処理することができ、総処理能力を効果的に倍増させ、バッチジョブの終了にかかる時間を大幅に短縮することができます。

これらのスレッドに最大制限を設定することは、パフォーマンスを最適化する強力な方法です。 また、プロセッサ・コア間のワークロードをバランスさせることで、アプリケーションがハードウェアの潜在能力をフルに活用できるようにします。 このアプローチは、効率を最大化し、アプリケーションのスムーズな実行を維持しながら、可能な限り最速の結果を提供します。

IronBarcodeはこの制限をコントロールする簡単な方法を提供し、最適なマシンパフォーマンスを実現します。 次のセクションでは、これらのスレッド制限を簡単に設定する方法を示します。



最大並列スレッドの設定

この例では、シングルスレッドではなくマルチスレッドプロセスを使用することのスケーラビリティと効率性を説明するために、大きなバーコード画像のセットを使用します。 画像フォルダはこちらからダウンロードできます

複数のスレッドを使用するようにIronBarcodeを設定するには、まず新しいBarcodeReaderOptionsオブジェクトをMultithreadedをtrueに設定してインスタンス化します。 その後、MaxParallelThreads プロパティに整数値を代入して設定します。 デフォルトでは、MaxParallelThreads は 4 に設定されています。

設定を行った後、多数のBarCode画像をフォルダからインポートします。 次に、ループを使用して、Readメソッドを使用して、ファイルパスと設定されたBarcodeReaderOptionsを渡し、バーコード画像ディレクトリを読み取ります。 最後に、バーコードの値とタイプは、BarCodeResultsにアクセスすることで表示されます。

:path=/static-assets/barcode/content-code-examples/how-to/set-max-parallel-thread.cs
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);

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

Imports IronBarCode
Imports System
Imports System.IO

Dim maxParallelThreads As Integer = 4

Dim optionsFaster As New BarcodeReaderOptions With {
    .Multithreaded = True,
    .MaxParallelThreads = maxParallelThreads
}

' Dynamically get the "images" folder in the current directory
Dim folderPath As String = Path.Combine(Directory.GetCurrentDirectory(), "images")

' Retrieve all JPG files in the directory
Dim pdfFiles = Directory.GetFiles(folderPath, "*.jpg")

For Each file In pdfFiles
    ' Read the barcode
    Dim results = BarcodeReader.Read(file)

    For Each result In results
        ' Show the type and value for every barcode found
        Console.WriteLine($"Value: {result.Value}, Type: {result.BarcodeType}")
    Next
Next
$vbLabelText   $csharpLabel

出力

マルチスレッド出力

コンソール出力に示すように、対応する各画像の BarCode 値とタイプを表示します。

適切な最大並列スレッドを設定する

Multithreaded プロパティが true に設定されている場合、MaxParallelThreads プロパティのデフォルトは 4 です。MaxParallelThreads に割り当てられる整数に難しい制限はありませんが、ハードウェアの論理コア容量よりも高い値を設定すると、実際にパフォーマンスが低下する可能性があります。 これは、プロセッサが過度なコンテキスト・スイッチングを処理できず、スピードよりもオーバーヘッドになる可能性があるためです。 そのため、MaxParallelThreadsの正しい値はコンピュータの仕様に依存し、開発者は自分の環境に最適な値を見つけるためにテストする必要があります。

この例では、上記と同じマルチスレッド シナリオを紹介しますが、Environment.ProcessorCount を使用して利用可能なすべてのスレッドを利用する場合と、デフォルト値の 4 を比較するタイマーを配置します。 私たちの場合、32個の論理プロセッサを持つコンピュータを使用しているので、MaxParallelThreadsは32に設定されます。

:path=/static-assets/barcode/content-code-examples/how-to/set-max-parallel-thread-performance.cs
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);

    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");
Imports IronBarCode
Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Linq

' Set the max parallel threads to the number of processor cores
Dim maxParallelThreads As Integer = Environment.ProcessorCount

Dim optionsFaster As New BarcodeReaderOptions With {
    .Multithreaded = True,
    .MaxParallelThreads = maxParallelThreads,
    .ExpectMultipleBarcodes = True
}

' Start timing the process
Dim stopwatch As Stopwatch = Stopwatch.StartNew()
' Dynamically get the "images" folder in the current directory
Dim folderPath As String = Path.Combine(Directory.GetCurrentDirectory(), "images")

' Check if directory exists to prevent crashes
If Not Directory.Exists(folderPath) Then
    Console.WriteLine($"Error: The directory '{folderPath}' does not exist.")
    Return
End If

' Get all JPG files in the directory
Dim pdfFiles As String() = Directory.GetFiles(folderPath, "*.jpg")

For Each file As String In pdfFiles
    ' Read the barcode
    Dim results = BarcodeReader.Read(file)

    If results.Any() Then
        Console.WriteLine($"Barcode(s) found in: {Path.GetFileName(file)}")
        For Each result In results
            Console.WriteLine($"  Value: {result.Value}, Type: {result.BarcodeType}")
        Next
    End If
Next

stopwatch.Stop()

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

出力

4つのスレッドによる処理時間

4 プロセッサ.

処理時間は84秒です。

環境の ProcessorCount を使用した処理時間

32 プロセッサ.

ご覧のように、この操作の処理時間は53秒で、4スレッドだけで実行するよりも大幅に高速です。 ただし、ホスト・プロセッサに依存するため、より多くのスレッドを使用してもパフォーマンスの向上が保証されるわけではないことに注意してください。 一般的な経験則では、使用可能なプロセッサの最大数から1を引いた数を使用し、他のシステム操作に使用可能なスレッドが1つ残るようにします。

プロジェクト環境は、マルチスレッドを許可するように設定されていなければなりません。 そうでなければ、Multithreadedtrueに設定し、MaxParallelThreadsを増やしても、処理速度は向上せず、むしろ低下する可能性があります。

よくある質問

IronBarcodeで最大並列スレッドを設定する目的は何ですか?

最大並列スレッドを設定すると、特に大量のバーコードを一括処理する場合に、システムリソースを効率的に使用してバーコード生成のパフォーマンスを最適化できます。

IronBarcodeの最大並列スレッドはどのように設定できますか?

IronBarcodeの最大並列スレッドを設定するには、C#コードの適切なメソッドを使用して、バーコード生成タスクに必要なスレッド数を設定します。

バーコードの一括作成のパフォーマンスを最適化することが重要なのはなぜですか?

バルクバーコード作成のパフォーマンスを最適化することで、プロセスが効率的かつ高速になり、大量のバーコードを生成するのに必要な時間とリソースが削減されます。

IronBarcodeで並列処理を使用する利点は何ですか?

IronBarcodeの並列処理は、複数のCPUコアを利用することでバーコード生成を高速化し、アプリケーションのパフォーマンス向上と大規模バーコードタスクの処理時間短縮につながります。

並列スレッドを多く設定しすぎると、パフォーマンスに悪影響がありますか?

そうですね。並列スレッドを多く設定しすぎると、リソースの競合やオーバーヘッドの増加につながり、パフォーマンスが低下する可能性があります。システムの能力に合ったバランスの取れた構成を見つけることが重要です。

並列スレッド数を選択する際に考慮すべき点は?

利用可能なCPUコア数、システムの作業負荷、バーコード生成タスクの性質などを考慮する必要があります。最適な設定を見つけるために、さまざまな設定を試してみるのが一番です。

IronBarcodeの並列スレッドのデフォルト設定はありますか?

IronBarcodeには並列スレッドのデフォルト設定がありますが、最高のパフォーマンスを達成するために、特定のアプリケーションのニーズに基づいてこの設定をカスタマイズすることをお勧めします。

IronBarcodeはどのようにスレッド管理を行うのですか?

IronBarcodeは.NETのスレッド機能を活用して並列処理を管理し、開発者がスレッド数を指定してパフォーマンスを効率的に最適化できるようにします。

実行中に並列スレッド数を動的に変更できますか?

実行中に並列スレッド数を動的に変更することは、不整合を引き起こし、パフォーマンスの安定性に影響を与える可能性があるため、推奨されない場合があります。バーコード生成プロセスを開始する前に設定を行うのが最善です。

BarCode 生成で最大並列スレッドを設定する一般的なユースケースは何ですか?

一般的な使用例としては、在庫管理システム、小売 POS システム、大量のバーコードを迅速に処理することが重要な物流アプリケーションなど、高速バーコード生成を必要とするアプリケーションが挙げられます。

カーティス・チャウ
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。

準備はできましたか?
Nuget ダウンロード 2,070,733 | バージョン: 2026.2 リリース