public void index() {
int threads = Math.max(1, Runtime.getRuntime().availableProcessors() - 1);
- this.executorService = Executors.newFixedThreadPool(threads, new ThreadFactoryBuilder().setNameFormat("FileIndexer-%d").build());
+ this.executorService = Executors.newFixedThreadPool(threads, new ThreadFactoryBuilder()
+ .setNameFormat("FileIndexer-%d")
+ .setDaemon(true)
+ .build());
progressReport = new ProgressReport("Report about progress of file indexation", TimeUnit.SECONDS.toMillis(10));
progressReport.start("Index files");
indexFiles(moduleFileSystemInitializer.sources(), InputFile.Type.MAIN, progress);
indexFiles(moduleFileSystemInitializer.tests(), InputFile.Type.TEST, progress);
- waitForTasksToComplete();
+ waitForTasksToComplete(progressReport);
progressReport.stop(progress.count() + " " + pluralizeFiles(progress.count()) + " indexed");
}
}
- private void waitForTasksToComplete() {
+ private void waitForTasksToComplete(ProgressReport report) {
executorService.shutdown();
for (Future<Void> task : tasks) {
try {
task.get();
} catch (ExecutionException e) {
// Unwrap ExecutionException
+ stopAsap(report);
throw e.getCause() instanceof RuntimeException ? (RuntimeException) e.getCause() : new IllegalStateException(e.getCause());
} catch (InterruptedException e) {
+ stopAsap(report);
throw new IllegalStateException(e);
}
}
}
+ private void stopAsap(ProgressReport report) {
+ report.stop(null);
+ executorService.shutdownNow();
+ try {
+ executorService.awaitTermination(5, TimeUnit.SECONDS);
+ } catch (InterruptedException e1) {
+ // ignore, what's important is the original exception
+ }
+ }
+
private static String pluralizeFiles(int count) {
return count == 1 ? "file" : "files";
}
*/
package org.sonar.scanner.util;
+import javax.annotation.Nullable;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
private final long period;
private String message = "";
private final Thread thread;
- private String stopMessage = "";
+ private String stopMessage = null;
public ProgressReport(String threadName, long period) {
this.period = period;
break;
}
}
- log(stopMessage);
+ if (stopMessage != null) {
+ log(stopMessage);
+ }
}
public void start(String startMessage) {
this.message = message;
}
- public void stop(String stopMessage) {
+ public void stop(@Nullable String stopMessage) {
this.stopMessage = stopMessage;
thread.interrupt();
try {