CAMEL-11750: SubmitOrderedCompletionService should avoid eating CPU cycles if a task is not ready and the returned delay value would be too fast, eg 1 nano which essentially would do a while loop with cpu active all the time.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/63c97d85 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/63c97d85 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/63c97d85 Branch: refs/heads/camel-2.18.x Commit: 63c97d8505ec780a9777c68fa7afbe8f40a4ac90 Parents: ab745dd Author: Claus Ibsen <davscl...@apache.org> Authored: Fri Sep 8 10:33:58 2017 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Fri Sep 8 10:37:11 2017 +0200 ---------------------------------------------------------------------- .../concurrent/SubmitOrderedCompletionService.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/63c97d85/camel-core/src/main/java/org/apache/camel/util/concurrent/SubmitOrderedCompletionService.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/util/concurrent/SubmitOrderedCompletionService.java b/camel-core/src/main/java/org/apache/camel/util/concurrent/SubmitOrderedCompletionService.java index 7b63480..09d02bf 100644 --- a/camel-core/src/main/java/org/apache/camel/util/concurrent/SubmitOrderedCompletionService.java +++ b/camel-core/src/main/java/org/apache/camel/util/concurrent/SubmitOrderedCompletionService.java @@ -63,7 +63,19 @@ public class SubmitOrderedCompletionService<V> implements CompletionService<V> { public long getDelay(TimeUnit unit) { // if the answer is 0 then this task is ready to be taken - return unit.convert(id - index.get(), TimeUnit.MILLISECONDS); + long answer = id - index.get(); + if (answer <= 0) { + return answer; + } + // okay this task is not ready yet, and we don't really know when it would be + // so we have to return a delay value of one time unit + if (TimeUnit.NANOSECONDS == unit) { + // okay this is too fast so use a little more delay to avoid CPU burning cycles + answer = unit.convert(1, TimeUnit.MICROSECONDS); + } else { + answer = unit.convert(1, unit); + } + return answer; } @SuppressWarnings("unchecked")