diff --git a/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java b/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java index 4b54f527892..17e9c54fe94 100644 --- a/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java +++ b/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java @@ -78,6 +78,9 @@ import sun.net.ResourceManager; import sun.net.ext.ExtendedSocketOptions; import sun.net.util.IPAddressUtil; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.NANOSECONDS; + /** * An implementation of DatagramChannels. */ @@ -497,7 +500,12 @@ class DatagramChannelImpl if (nanos == 0) { millis = -1; } else { - millis = TimeUnit.NANOSECONDS.toMillis(nanos); + millis = NANOSECONDS.toMillis(nanos); + if (nanos > MILLISECONDS.toNanos(millis)) { + // Round up any excess nanos to the nearest millisecond to + // avoid parking for less than requested. + millis++; + } } Net.poll(getFD(), event, millis); } diff --git a/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java b/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java index cb42710afd6..b9885a36fa8 100644 --- a/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java +++ b/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java @@ -182,6 +182,11 @@ public final class NioSocketImpl extends SocketImpl implements PlatformSocketImp millis = -1; } else { millis = NANOSECONDS.toMillis(nanos); + if (nanos > MILLISECONDS.toNanos(millis)) { + // Round up any excess nanos to the nearest millisecond to + // avoid parking for less than requested. + millis++; + } } Net.poll(fd, event, millis); } diff --git a/src/java.base/share/classes/sun/nio/ch/SelChImpl.java b/src/java.base/share/classes/sun/nio/ch/SelChImpl.java index 9924ed1cc51..5abe4d41ac0 100644 --- a/src/java.base/share/classes/sun/nio/ch/SelChImpl.java +++ b/src/java.base/share/classes/sun/nio/ch/SelChImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -28,6 +28,8 @@ package sun.nio.ch; import java.nio.channels.Channel; import java.io.FileDescriptor; import java.io.IOException; + +import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.NANOSECONDS; /** @@ -90,6 +92,11 @@ public interface SelChImpl extends Channel { millis = -1; } else { millis = NANOSECONDS.toMillis(nanos); + if (nanos > MILLISECONDS.toNanos(millis)) { + // Round up any excess nanos to the nearest millisecond to + // avoid parking for less than requested. + millis++; + } } Net.poll(getFD(), event, millis); }