From 87dd72e59b5084e48881cc55ca5a343dc5774c62 Mon Sep 17 00:00:00 2001 From: Jakub Valtar Date: Sat, 7 May 2016 12:21:19 +0200 Subject: [PATCH] PPS: wait for running callbacks to finish before publishing new result --- .../mode/java/pdex/PreprocessingService.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/java/src/processing/mode/java/pdex/PreprocessingService.java b/java/src/processing/mode/java/pdex/PreprocessingService.java index c156a87b1..b3f2d8d47 100644 --- a/java/src/processing/mode/java/pdex/PreprocessingService.java +++ b/java/src/processing/mode/java/pdex/PreprocessingService.java @@ -112,6 +112,7 @@ public class PreprocessingService { private void mainLoop() { running = true; PreprocessedSketch prevResult = null; + CompletableFuture runningCallbacks = null; Messages.log("PPS: Hi!"); while (running) { try { @@ -126,8 +127,18 @@ public class PreprocessingService { prevResult = preprocessSketch(prevResult); + // Wait until callbacks finish before firing new wave + // If new request arrives while waiting, break out and start preprocessing + while (requestQueue.isEmpty() && runningCallbacks != null) { + try { + runningCallbacks.get(10, TimeUnit.MILLISECONDS); + runningCallbacks = null; + } catch (TimeoutException e) { } + } + synchronized (requestLock) { if (requestQueue.isEmpty()) { + runningCallbacks = lastCallback; Messages.log("PPS: Done"); preprocessingTask.complete(prevResult); }