Aynı Anda Birden Fazla İş Parçacığından Hücre Değerlerini Okuma
Birden fazla iş parçacığında aynı anda hücre değerleri okumak için, Cells.setMultiThreadReading(boolean) değerini doğru olarak ayarlayın. Aksi takdirde yanlış hücre değerleri alabilirsiniz.
Aşağıdaki kod:
- Bir çalışma kitabı oluşturur.
- Bir çalışma sayfası ekler.
- Çalışma sayfasını dize değerleriyle doldurur.
- Sonra rastgele hücrelerden aynı anda değer okuyan iki iş parçacığı oluşturur. Okunan değerler doğru ise hiçbir şey olmaz. Okunan değerler yanlışsa bir mesaj görüntülenir.
Eğer bu satırı yorumlarsanız:
testWorkbook.getWorksheets().get(0).getCells().setMultiThreadReading(true);
o zaman aşağıdaki mesaj görüntülenir:
if (s !== "R" + row + "C" + col)
{
console.log("This message box will show up when cells read values are incorrect.");
}
Aksi takdirde, program herhangi bir mesaj göstermeden çalışır, bu da demek olur ki tüm hücrelerden okunan değerler doğrudur.
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(); |