複数のスレッドで同時に Cell の値を読み取る
Contents
[
Hide
]
複数のスレッドで同時にセル値を読み取る必要があるのは、一般的な要件です。この記事では、この目的で Aspose.Cells を使用する方法について説明します。
複数のスレッドで同時にセル値を読み取るには、次のように設定します。Worksheet.getCells().setMultiThreadReading()に真実.そうしないと、間違ったセル値が得られる可能性があります。マルチスレッドでは、セル値の書式設定などの一部の機能がサポートされていないことに注意してください。したがって、MultiThreadReading では、セルの元のデータのみにアクセスできます。マルチスレッド環境で、数値の Cell.getStringValue() などでセルの書式設定された値を取得しようとすると、予期しない結果または例外が発生する場合があります。
次のコード:
- ワークブックを作成します。
- ワークシートを追加します。
- ワークシートに文字列値を入力します。
- 次に、ランダム セルから値を同時に読み取る 2 つのスレッドを作成します。 読み取った値が正しい場合、何も起こりません。読み取った値が正しくない場合は、メッセージが表示されます。
この行にコメントすると:
testWorkbook.getWorksheets().get(0).getCells().setMultiThreadReading(true);
次のメッセージが表示されます。
if (s.equals("R" + row + "C" + col)!=true)
{
System.out.println("This message box will show up when cells read values are incorrect.");
}
それ以外の場合、セルから読み取ったすべての値が正しいことを意味するメッセージを表示せずにプログラムが実行されます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
public abstract class ThreadProc implements Runnable { | |
boolean isRunning = true; | |
Workbook testWorkbook; | |
Random r = new Random(); | |
public ThreadProc(Workbook workbook) { | |
this.testWorkbook = workbook; | |
} | |
public int randomNext(int Low, int High) { | |
int R = r.nextInt(High - Low) + Low; | |
return R; | |
} | |
public void kill() { | |
this.isRunning = false; | |
} | |
public void run() { | |
while (this.isRunning) { | |
int row = randomNext(0, 10000); | |
int col = randomNext(0, 100); | |
//Commonly you can only access Cell.Value instead of Cell.StringValue. | |
//In this demo all cell values are string, so Cell.StringValue is same with Cell.Value here and can be used. | |
//For other values such as numeric, if you call Cell.StringValue here, you may get unexpected result or exception here. | |
//It is because that Cell.getStringValue() will format those values according to the number format but the formatting process | |
//does not support multi-threads. | |
String s = testWorkbook.getWorksheets().get(0).getCells().get(row, col).getStringValue(); | |
if (s.equals("R" + row + "C" + col) != true) { | |
System.out.println("This message box will show up when cells read values are incorrect."); | |
} | |
} | |
} | |
} | |
// Main.Java | |
static void TestMultiThreadingRead() throws Exception { | |
Workbook testWorkbook = new Workbook(); | |
testWorkbook.getWorksheets().clear(); | |
testWorkbook.getWorksheets().add("Sheet1"); | |
for (int row = 0; row < 10000; row++) | |
for (int col = 0; col < 100; col++) | |
testWorkbook.getWorksheets().get(0).getCells().get(row, col).setValue("R" + row + "C" + col); | |
//Commenting this line will show a pop-up message | |
testWorkbook.getWorksheets().get(0).getCells().setMultiThreadReading(true); | |
ThreadProc tp = new ThreadProc(testWorkbook); | |
Thread myThread1 = new Thread(tp); | |
myThread1.start(); | |
Thread myThread2 = new Thread(tp); | |
myThread2.start(); | |
Thread.currentThread().sleep(5*1000); | |
tp.kill(); | |
} |