|
19 | 19 | import com.sonar.sslr.api.RecognitionException; |
20 | 20 | import java.io.IOException; |
21 | 21 | import java.util.List; |
22 | | -import java.util.concurrent.ForkJoinPool; |
| 22 | +import java.util.concurrent.CompletableFuture; |
| 23 | +import java.util.concurrent.CompletionException; |
| 24 | +import java.util.concurrent.Executors; |
23 | 25 | import java.util.concurrent.atomic.AtomicInteger; |
24 | 26 | import java.util.stream.Stream; |
25 | 27 | import org.slf4j.Logger; |
@@ -57,21 +59,32 @@ protected void processFiles(List<PythonInputFile> files, SensorContext context, |
57 | 59 | getFilesStream(files).forEach(file -> processFile(context, file, progressReport, numScannedWithoutParsing)); |
58 | 60 | return; |
59 | 61 | } |
60 | | - var pool = new ForkJoinPool(numberOfThreads); |
| 62 | + var executor = Executors.newWorkStealingPool(numberOfThreads); |
61 | 63 | try { |
62 | | - pool.submit(() -> getFilesStream(files).forEach(file -> processFile(context, file, progressReport, numScannedWithoutParsing))) |
63 | | - .join(); |
| 64 | + var allTasks = CompletableFuture.allOf( |
| 65 | + files.stream() |
| 66 | + .map(file -> CompletableFuture.runAsync(() -> processFile(context, file, progressReport, numScannedWithoutParsing), executor)) |
| 67 | + .toArray(CompletableFuture[]::new) |
| 68 | + ); |
| 69 | + allTasks.join(); |
| 70 | + } catch (CompletionException e) { |
| 71 | + var cause = e.getCause(); |
| 72 | + if (cause instanceof RuntimeException runtimeException) { |
| 73 | + throw runtimeException; |
| 74 | + } else if (cause instanceof Error error) { |
| 75 | + throw error; |
| 76 | + } else { |
| 77 | + throw e; |
| 78 | + } |
64 | 79 | } finally { |
65 | | - pool.shutdown(); |
| 80 | + executor.shutdown(); |
66 | 81 | } |
67 | 82 | } |
68 | 83 |
|
69 | 84 | protected abstract void logStart(int numThreads); |
70 | 85 |
|
71 | 86 | protected Stream<PythonInputFile> getFilesStream(List<PythonInputFile> files) { |
72 | | - return getNumberOfThreads(context) == 1 |
73 | | - ? files.stream() |
74 | | - : files.parallelStream(); |
| 87 | + return files.stream(); |
75 | 88 | } |
76 | 89 |
|
77 | 90 | private void processFile(SensorContext context, PythonInputFile file, MultiFileProgressReport progressReport, AtomicInteger numScannedWithoutParsing) { |
|
0 commit comments