From de3de300cfedda75e614bf9a929cfec2020b40f0 Mon Sep 17 00:00:00 2001 From: Joel Therrien Date: Fri, 26 Apr 2019 11:04:03 -0700 Subject: [PATCH] Change how parallel trees are trained to be more robust to Threads getting terminated. This will hopefully make the package more stable on cluster systems, where sometimes the forests immediately stall. --- .../randomforest/tree/ForestTrainer.java | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/main/java/ca/joeltherrien/randomforest/tree/ForestTrainer.java b/src/main/java/ca/joeltherrien/randomforest/tree/ForestTrainer.java index 5d721f1..6b8b285 100644 --- a/src/main/java/ca/joeltherrien/randomforest/tree/ForestTrainer.java +++ b/src/main/java/ca/joeltherrien/randomforest/tree/ForestTrainer.java @@ -34,6 +34,7 @@ import java.util.UUID; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -135,15 +136,15 @@ public class ForestTrainer { executorService.execute(worker); } - executorService.shutdown(); - int prevNumberTreesSet = -1; - while(!executorService.isTerminated()){ - - try{ - Thread.sleep(500); + while(true){ + try { + if (executorService.awaitTermination(5, TimeUnit.SECONDS)) break; } catch (InterruptedException e) { - // do nothing; who cares? + System.err.println("There was an InterruptedException while waiting for the forest to finish training; this is unusual but on its own shouldn't be a problem."); + System.err.println("Please send a bug report about it to joelt@sfu.ca"); + e.printStackTrace(); + // do nothing; this shouldn't be an issue } if(displayProgress) { @@ -195,15 +196,15 @@ public class ForestTrainer { executorService.execute(worker); } - executorService.shutdown(); - int prevNumberTreesSet = -1; - while(!executorService.isTerminated()){ - - try{ - Thread.sleep(100); + while(true){ + try { + if (executorService.awaitTermination(5, TimeUnit.SECONDS)) break; } catch (InterruptedException e) { - // do nothing; who cares? + System.err.println("There was an InterruptedException while waiting for the forest to finish training; this is unusual but on its own shouldn't be a problem."); + System.err.println("Please send a bug report about it to joelt@sfu.ca"); + e.printStackTrace(); + // do nothing; this shouldn't be an issue } if(displayProgress) {