JDK-8262437 : Instant.now() returns same instant after first call
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.time
  • Affected Version: 8,11,15,17
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_10
  • CPU: x86_64
  • Submitted: 2021-02-25
  • Updated: 2021-05-25
  • Resolved: 2021-02-26
Related Reports
Duplicate :  
Description
ADDITIONAL SYSTEM INFORMATION :
I am using Intellij IDEA 2020.1 and installed openjdk-15 via the Project Structure dialogue. The same error happens with a manually installed version (11.05) as well.

A DESCRIPTION OF THE PROBLEM :
If I use Instant.now() repeatedly, later calls return the same value even if time should have passed already.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Initialize two variables with Instant.now() and print one after each other.

Instant now1 = Instant.now();
System.out.println(now1);
Instant now2 = Instant.now();
System.out.println(now2);


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The variables should be different.
ACTUAL -
The variables are the same:
2021-02-25T15:49:57.512378100Z
2021-02-25T15:49:57.512378100Z

---------- BEGIN SOURCE ----------
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;

import java.time.Instant;

import static org.assertj.core.api.Assertions.assertThat;

class InstantTest {

    @Test
    public void succeedsIfRunInIsolation() {
        assertThat(Instant.now()).isNotEqualTo(Instant.now());
    }

    @RepeatedTest(10)
    public void allButFirstFailIfRunInIsolation() {
        assertThat(Instant.now()).isNotEqualTo(Instant.now());
    }

    @Test
    void failsAlways() {
        Instant now1 = Instant.now();
        System.out.println(now1);
        Instant now2 = Instant.now();
        System.out.println(now2);

        assertThat(now1).isNotEqualTo(now2);
    }

}
---------- END SOURCE ----------

FREQUENCY : always



Comments
Closing as a duplicate of JDK-8180466. Clock.systemUTC() has lower resolution on Windows that results in the behavior described in this issue. Please take a look at the comment section of 8180466, where the rationale for not making the resolution finer on Windows is described.
26-02-2021

The observations on Windows 10: JDK 8: Failed, returns the same instant. JDK 11: Failed. JDK 15: Failed. JDK 17ea+6: Failed.
26-02-2021