Cell 同时读取多个线程中的值
Contents
[
Hide
]
需要同时读取多个线程中的单元格值是一个常见的需求。本文介绍了如何为此目的使用 Aspose.Cells。
要同时读取多个线程中的单元格值,请设置Worksheet.getCells().setMultiThreadReading()到真的.如果不这样做,您可能会得到错误的单元格值。请注意,多线程不支持某些功能,例如格式化单元格值。所以 MultiThreadReading 只允许您访问单元格的原始数据。在多线程环境下,如果您尝试获取单元格的格式化值,例如通过 Cell.getStringValue() 获取数值,您可能会得到意外的结果或异常。
以下代码:
- 创建工作簿。
- 添加工作表。
- 使用字符串值填充工作表。
- 然后它创建两个同时从随机单元格中读取值的线程。 如果读取的值正确,则不会发生任何事情。如果读取的值不正确,则会显示一条消息。
如果您评论此行:
testWorkbook.getWorksheets().get(0).getCells().setMultiThreadReading(true);
然后显示以下消息:
if (s.equals("R" + row + "C" + col)!=true)
{
System.out.println("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
// 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(); | |
} |