From 1eb4a04a789c8c3a4c51f9e309d37ceb096ce4db Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Tue, 10 Dec 2024 14:19:59 +0800 Subject: [PATCH] Always check `available()` in `InputPumper` to avoid burning CPU (#4095) Attempts to fix https://github.com/com-lihaoyi/mill/discussions/4092 Instead of checking `src.available() == 0`, we check for `src.read(buffer) == 0`, which should provide a more accurate measure --- main/client/src/mill/main/client/InputPumper.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/main/client/src/mill/main/client/InputPumper.java b/main/client/src/mill/main/client/InputPumper.java index ef3d8e7f6d7..bae7005a5a8 100644 --- a/main/client/src/mill/main/client/InputPumper.java +++ b/main/client/src/mill/main/client/InputPumper.java @@ -36,9 +36,7 @@ public void run() { byte[] buffer = new byte[1024]; try { while (running) { - if (!runningCheck.getAsBoolean()) { - running = false; - } else if (checkAvailable && src.available() == 0) Thread.sleep(1); + if (!runningCheck.getAsBoolean()) running = false; else { int n; try { @@ -46,9 +44,9 @@ public void run() { } catch (Exception e) { n = -1; } - if (n == -1) { - running = false; - } else { + if (n == -1) running = false; + else if (n == 0) Thread.sleep(1); + else { try { dest.write(buffer, 0, n); dest.flush();