Hi.
We are trying to improve perfomance of reading book’s model and we’ve tried to use multithreading. But it is failed.
Following example tries to reading values of cells in several threads. It is works when we try to work in one thread, but when we are increasing count of parallel working, chance to fail increased.
Is it possible to work same way with book?
public static Stream<Arguments> multiThreading_source() {
return IntStream.range(19, 98).mapToObj(value -> Arguments.of(19, value - 19 + 1));
}
@ParameterizedTest
@MethodSource("multiThreading_source")
void multiThreading(int from, int count) throws Exception {
System.out.println("Creating model");
Workbook workbook = new Workbook("E://in.xlsx");
System.out.println("Reading");
long start = System.currentTimeMillis();
ExecutorService executorService = Executors.newFixedThreadPool(10);
CompletionService<String> completionService = new ExecutorCompletionService<String>(executorService);
Cells cells = workbook.getWorksheets().get(0).getCells();
for (int i = 0; i < count; i++) {
completionService.submit(new RowReader(cells, i + from, "#" + (i + 1)));
}
List<String> rows = new ArrayList<>(count);
int received = 0;
boolean errors = false;
while (received < count && !errors) {
Future<String> resultFuture = completionService.take(); //blocks if none available
try {
rows.add(resultFuture.get());
received++;
} catch (Exception e) {
//log
System.err.println(e);
errors = true;
}
}
System.out.println("Takes " + (System.currentTimeMillis() - start) + "ms");
for (String row : rows) {
System.out.println(row);
}
assertFalse(errors);
}
in.zip (253.2 KB)