Lettura di valori di celle in thread multipli contemporaneamente
Come leggere i valori della cella in più thread contemporaneamente con Aspose.Cells for Java
Per leggere i valori delle celle in più di un thread contemporaneamente, impostare Worksheet.getCells().setMultiThreadReading() su true. Se non lo si fa, potrebbero essere ottenuti valori errati delle celle. Si noti che alcune funzionalità come la formattazione dei valori delle celle non sono supportate per i thread multipli. Quindi, la lettura multithread abilita solo l’accesso ai dati originali delle celle. In un ambiente multi-thread se si cerca di ottenere il valore formattato della cella, ad esempio tramite Cell.getStringValue() per i valori numerici, potrebbe essere ottenuto un risultato inaspettato o un’eccezione.
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.equals("R" + row + "C" + col)!=true)
{
System.out.println("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.
// 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(); | |
} |