複数のスレッドで同時にセル値を読み取る
Contents
[
Hide
]
複数のスレッドで同時にセル値を読み取る必要がある場合がよくあります。この記事ではその目的で Aspose.Cells の使用方法を説明します。
複数のスレッドでセル値を同時に読み取るには、Cells.setMultiThreadReading(boolean)をtrueに設定します。そうしないと、誤ったセル値を取得する可能性があります。
次のコード:
- ワークブックを作成します。
- ワークシートを追加します。
- 文字列値でワークシートを埋めます。
- 次に、ランダムなセルから同時に値を読み取る2つのスレッドを作成します。 読み取った値が正しい場合、何も起こりません。読み取った値が間違っている場合は、メッセージが表示されます。
この行をコメントアウトすると、次のメッセージが表示されます:
testWorkbook.getWorksheets().get(0).getCells().setMultiThreadReading(true);
それ以外の場合、すべてのセルから読み取った値が正しいことを意味するメッセージが表示されずにプログラムが実行されます。
if (s !== "R" + row + "C" + col)
{
console.log("This message box will show up when cells read values are incorrect.");
}
それ以外の場合、セルから読み取ったすべての値が正しい場合、プログラムはメッセージを表示せずに実行されます。
This file contains hidden or 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
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
const dataDir = path.join(__dirname, "data"); | |
let testWorkbook; | |
const threadLoop = () => { | |
const random = Math.random; | |
while (true) { | |
try { | |
const row = Math.floor(random() * 10000); | |
const col = Math.floor(random() * 100); | |
const s = testWorkbook.getWorksheets().get(0).getCells().get(row, col).getStringValue(); | |
if (s !== "R" + row + "C" + col) { | |
console.log("This message will show up when cells read values are incorrect."); | |
} | |
} catch (e) {} | |
} | |
}; | |
const testMultiThreadingRead = () => { | |
testWorkbook = new AsposeCells.Workbook(); | |
testWorkbook.getWorksheets().clear(); | |
testWorkbook.getWorksheets().add("Sheet1"); | |
for (let row = 0; row < 10000; row++) | |
for (let col = 0; col < 100; col++) | |
testWorkbook.getWorksheets().get(0).getCells().get(row, col).setValue("R" + row + "C" + col); | |
// Uncommenting this line will enable multi-threaded reading | |
//testWorkbook.getWorksheets().get(0).getCells().setMultiThreadReading(true); | |
const myThread1 = setInterval(threadLoop, 0); | |
const myThread2 = setInterval(threadLoop, 0); | |
setTimeout(() => { | |
clearInterval(myThread1); | |
clearInterval(myThread2); | |
}, 5 * 1000); | |
}; | |
testMultiThreadingRead(); |