Skip to content

Commit

Permalink
Reflects review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
naotoj committed Jul 18, 2024
1 parent 4f8f118 commit 95085cc
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions src/java.base/share/classes/java/lang/Process.java
Original file line number Diff line number Diff line change
Expand Up @@ -457,15 +457,24 @@ public final BufferedWriter outputWriter(Charset charset) {
* @since 1.8
*/
public boolean waitFor(long timeout, TimeUnit unit)
throws InterruptedException {
Objects.requireNonNull(unit, "unit"); // throw NPE before other conditions
throws InterruptedException
{
long remainingNanos = unit.toNanos(timeout); // throw NPE before other conditions

if (hasExited())
return true;
if (timeout <= 0)
return false;

return waitForNanos(unit.toNanos(timeout));
long deadline = System.nanoTime() + remainingNanos;
do {
Thread.sleep(Math.min(TimeUnit.NANOSECONDS.toMillis(remainingNanos) + 1, 100));
if (hasExited())
return true;
remainingNanos = deadline - System.nanoTime();
} while (remainingNanos > 0);

return false;
}

/**
Expand All @@ -492,28 +501,9 @@ public boolean waitFor(long timeout, TimeUnit unit)
* @throws NullPointerException if duration is null
* @since 24
*/
public boolean waitFor(Duration duration)
throws InterruptedException {
Objects.requireNonNull(duration, "duration"); // throw NPE before other conditions

if (hasExited())
return true;
if (!duration.isPositive())
return false;

return waitForNanos(TimeUnit.NANOSECONDS.convert(duration));
}

private boolean waitForNanos(long remainingNanos) throws InterruptedException {
long deadline = System.nanoTime() + remainingNanos;
do {
Thread.sleep(Math.min(TimeUnit.NANOSECONDS.toMillis(remainingNanos) + 1, 100));
if (hasExited())
return true;
remainingNanos = deadline - System.nanoTime();
} while (remainingNanos > 0);

return false;
public boolean waitFor(Duration duration) throws InterruptedException {
Objects.requireNonNull(duration, "duration");
return waitFor(TimeUnit.NANOSECONDS.convert(duration), TimeUnit.NANOSECONDS);
}

/**
Expand Down

0 comments on commit 95085cc

Please sign in to comment.