最大並列スレッドの設定方法
大量の BarCode を読み取る場合、シングルスレッド処理に頼るとパフォーマンスのボトルネックが発生し、スケーラビリティが制限される可能性があります。 しかし、並列スレッドを使用することで、アプリケーションは複数の画像を同時に処理することができ、総処理能力を効果的に倍増させ、バッチジョブの終了にかかる時間を大幅に短縮することができます。
これらのスレッドに最大制限を設定することは、パフォーマンスを最適化する強力な方法です。 また、プロセッサ・コア間のワークロードをバランスさせることで、アプリケーションがハードウェアの潜在能力をフルに活用できるようにします。 このアプローチは、効率を最大化し、アプリケーションのスムーズな実行を維持しながら、可能な限り最速の結果を提供します。
IronBarcodeはこの制限をコントロールする簡単な方法を提供し、最適なマシンパフォーマンスを実現します。 次のセクションでは、これらのスレッド制限を簡単に設定する方法を示します。
C#で最大並列スレッドを設定する方法
- バーコードを読み取るための最大並列スレッドを設定するIronBarcode C#ライブラリをダウンロードする。
- 指定されたフォルダから BarCode を読み込みます。
- 新しい `BarcodeReaderOptions` オブジェクトをインスタンス化します。
- ` Multithreaded ` を true に設定し、` MaxParallelThreads ` に整数を指定してください。
- `BarcodeResults`で結果を印刷して表示します。
最大並列スレッドの設定
この例では、シングルスレッドではなくマルチスレッドプロセスを使用することのスケーラビリティと効率性を説明するために、大きなバーコード画像のセットを使用します。 画像フォルダはこちらからダウンロードできます。
IronBarcode が複数のスレッドを使用するように構成するには、まず、BarcodeReaderOptions を true に設定して新しい Multithreaded オブジェクトをインスタンス化します。 その後、整数値を割り当てることによって、MaxParallelThreads プロパティが設定されます。 デフォルトでは、MaxParallelThreads は 4 に設定されています。
設定を行った後、多数のBarCode画像をフォルダからインポートします。 次に、ループを使用して、ファイル パスと構成された BarcodeReaderOptions を渡して、バーコード イメージ ディレクトリを Read メソッドで読み取ります。 最後に、BarcodeResults にアクセスしてバーコードの値と種類を表示します。
:path=/static-assets/barcode/content-code-examples/how-to/set-max-parallel-thread.cs
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);
foreach (var result in results)
{
// Show the type and value for every barcode found
Console.WriteLine($"Value: {result.Value}, Type: {result.BarcodeType}");
}
}
Imports Google.Protobuf.WellKnownTypes
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
出力
コンソール出力に示すように、対応する各画像の BarCode 値とタイプを表示します。
適切な最大並列スレッドを設定する
Multithreaded プロパティが true に設定されている場合、MaxParallelThreads プロパティはデフォルトで 4 に設定されます。MaxParallelThreads に割り当てられる整数には厳密な制限はありませんが、ハードウェアの論理コア容量よりも高い値を設定すると、実際にはパフォーマンスが低下する可能性があります。 これは、プロセッサが過度なコンテキスト・スイッチングを処理できず、スピードよりもオーバーヘッドになる可能性があるためです。 したがって、MaxParallelThreadsの正しい値はコンピュータの仕様によって異なり、開発者はテストを行って自分の環境に最適な値を見つける必要があります。
この例では、上記と同じマルチスレッド シナリオを紹介しますが、タイマーを設定してデフォルト値の 4 と Environment.ProcessorCount を使用して利用可能なすべてのスレッドを活用します。 この例では、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")
出力
4つのスレッドによる処理時間
処理時間は84秒です。
環境の ProcessorCount を使用した処理時間
ご覧のように、この操作の処理時間は53秒で、4スレッドだけで実行するよりも大幅に高速です。 ただし、ホスト・プロセッサに依存するため、より多くのスレッドを使用してもパフォーマンスの向上が保証されるわけではないことに注意してください。 一般的な経験則では、使用可能なプロセッサの最大数から1を引いた数を使用し、他のシステム操作に使用可能なスレッドが1つ残るようにします。
Multithreaded を true に設定し、MaxParallelThreads を増やしても、処理速度は向上せず、むしろ低下する可能性があります。よくある質問
IronBarcodeで最大並列スレッドを設定する目的は何ですか?
最大並列スレッドを設定すると、特に大量のバーコードを一括処理する場合に、システムリソースを効率的に使用してバーコード生成のパフォーマンスを最適化できます。
IronBarcodeの最大並列スレッドはどのように設定できますか?
IronBarcodeの最大並列スレッドを設定するには、C#コードの適切なメソッドを使用して、バーコード生成タスクに必要なスレッド数を設定します。
バーコードの一括作成のパフォーマンスを最適化することが重要なのはなぜですか?
バルクバーコード作成のパフォーマンスを最適化することで、プロセスが効率的かつ高速になり、大量のバーコードを生成するのに必要な時間とリソースが削減されます。
IronBarcodeで並列処理を使用する利点は何ですか?
IronBarcodeの並列処理は、複数のCPUコアを利用することでバーコード生成を高速化し、アプリケーションのパフォーマンス向上と大規模バーコードタスクの処理時間短縮につながります。
並列スレッドを多く設定しすぎると、パフォーマンスに悪影響がありますか?
そうですね。並列スレッドを多く設定しすぎると、リソースの競合やオーバーヘッドの増加につながり、パフォーマンスが低下する可能性があります。システムの能力に合ったバランスの取れた構成を見つけることが重要です。
並列スレッド数を選択する際に考慮すべき点は?
利用可能なCPUコア数、システムの作業負荷、バーコード生成タスクの性質などを考慮する必要があります。最適な設定を見つけるために、さまざまな設定を試してみるのが一番です。
IronBarcodeの並列スレッドのデフォルト設定はありますか?
IronBarcodeには並列スレッドのデフォルト設定がありますが、最高のパフォーマンスを達成するために、特定のアプリケーションのニーズに基づいてこの設定をカスタマイズすることをお勧めします。
IronBarcodeはどのようにスレッド管理を行うのですか?
IronBarcodeは.NETのスレッド機能を活用して並列処理を管理し、開発者がスレッド数を指定してパフォーマンスを効率的に最適化できるようにします。
実行中に並列スレッド数を動的に変更できますか?
実行中に並列スレッド数を動的に変更することは、不整合を引き起こし、パフォーマンスの安定性に影響を与える可能性があるため、推奨されない場合があります。バーコード生成プロセスを開始する前に設定を行うのが最善です。
BarCode 生成で最大並列スレッドを設定する一般的なユースケースは何ですか?
一般的な使用例としては、在庫管理システム、小売 POS システム、大量のバーコードを迅速に処理することが重要な物流アプリケーションなど、高速バーコード生成を必要とするアプリケーションが挙げられます。

