I have a bunch of columns in the form of string arrays from a csv file. Now I want to take them apart. Since this parsing requires date parsing and other not-so-fast parsing methods, I thought of parallelism (I timed it, it takes some time). My simple approach:
Stream.of(columns).parallel().forEach(column -> result[column.index] = parseColumn(valueCache[column.index], column.type));
Columns contain ColumnDescriptor elements that simply have two attributes, the parsed column index and the type that determines how to parse it. Nothing more. the result is an Object array that accepts the resulting arrays.
The problem is that the parse function throws a ParseException, which I handle later in the call stack. Since we are here in parallel, we cannot just throw it away. What is the best way to handle this?
I have this solution, but I kind of read it. What would be the best way to do this?
final CompletableFuture<ParseException> thrownException = new CompletableFuture<>(); Stream.of(columns).parallel().forEach(column -> { try { result[column.index] = parseColumn(valueCache[column.index], column.type); } catch (ParseException e) { thrownException.complete(e); }}); if(thrownException.isDone()) //only can be done if there is a value set. throw thrownException.getNow(null);
Notes: I do not need all exceptions. If I disassemble them sequentially, I also get only one. So this is normal.
source share