8249367: JShell uses 100% of one core all the time

Workarounding busy wait while reading from NonBlocking.NonBlockingReaderInputStream wrapping NonBlockingPumpReader.

Reviewed-by: rfield, cstein
This commit is contained in:
Jan Lahoda 2020-07-16 11:30:11 +02:00
parent 507e54840f
commit 3770be7f70

View File

@ -28,6 +28,7 @@ package jdk.internal.jshell.tool;
import java.io.IOException;
import java.io.InputStream;
import java.util.function.Consumer;
import jdk.internal.org.jline.utils.NonBlockingInputStream;
public final class StopDetectingInputStream extends InputStream {
public static final int INITIAL_SIZE = 128;
@ -63,7 +64,19 @@ public final class StopDetectingInputStream extends InputStream {
if (currentState == State.CLOSED) {
break;
}
if ((read = input.read()) == (-1)) {
if (input instanceof NonBlockingInputStream) {
//workaround: NonBlockingPumpReader.read(), which should
//block until input is available, returns immediatelly,
//which causes busy-waiting for input, consuming
//unnecessary CPU resources. Using a timeout to avoid
//blocking read:
do {
read = ((NonBlockingInputStream) input).read(1000L);
} while (read == NonBlockingInputStream.READ_EXPIRED);
} else {
read = input.read();
}
if (read == (-1)) {
break;
}
if (read == 3 && getState() == State.BUFFER) {