Lettura di valori di celle in thread multipli contemporaneamente
Per leggere i valori delle celle in più di un thread contemporaneamente, imposta Cells.setMultiThreadReading(boolean) su true. Se non lo fai, potresti ottenere valori di cella sbagliati.
Il seguente codice:
- Crea un workbook.
- Aggiunge un foglio di lavoro.
- Popola il foglio di lavoro con valori di stringa.
- Quindi crea due thread che leggono contemporaneamente valori da celle casuali. Se i valori letti sono corretti, non succede nulla. Se i valori letti non sono corretti, viene visualizzato un messaggio.
Se si commenta questa riga:
testWorkbook.getWorksheets().get(0).getCells().setMultiThreadReading(true);
allora viene visualizzato il seguente messaggio:
if (s !== "R" + row + "C" + col)
{
console.log("This message box will show up when cells read values are incorrect.");
}
In caso contrario, il programma viene eseguito senza mostrare alcun messaggio, il che significa che tutti i valori letti dalle celle sono corretti.
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(); |