Lectura de valores de celda en múltiples hilos simultáneamente

Cómo leer valores de celda en hilos múltiples simultáneamente con Aspose.Cells for Java

Para leer valores de celda en más de un hilo simultáneamente, configure Worksheet.getCells().setMultiThreadReading() a true. De lo contrario, puede obtener valores de celda incorrectos. Tenga en cuenta que algunas funciones, como dar formato a los valores de las celdas, no son compatibles con múltiples hilos. Por lo tanto, la lectura en varios hilos solo le permite acceder a los datos originales de las celdas. En un entorno de varios hilos, si intenta obtener el valor formateado de la celda, como usando Cell.getStringValue() para valores numéricos, es posible que obtenga un resultado inesperado o una excepción.

El siguiente código:

  1. Crea un libro de trabajo.
  2. Agrega una hoja de cálculo.
  3. Rellena la hoja de cálculo con valores de cadena.
  4. Luego crea dos hilos que leen valores simultáneamente de celdas aleatorias. Si los valores leídos son correctos, no sucede nada. Si los valores leídos son incorrectos, se muestra un mensaje.

Si comentas esta línea:

testWorkbook.getWorksheets().get(0).getCells().setMultiThreadReading(true);

entonces se muestra el siguiente mensaje:

if (s.equals("R" + row + "C" + col)!=true)

{

    System.out.println("This message box will show up when cells read values are incorrect.");

}

De lo contrario, el programa se ejecuta sin mostrar ningún mensaje, lo que significa que todos los valores leídos de las celdas son correctos.

// 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();
}