System.currentTimeMillis() | Sleep Less Than One Millisecond

The issue was with the following code snippet:

MyEntity a = new MyEntity();
        a.setUid("foo" + System.currentTimeMillis());
        a.setName("my entity");
        entityService.save(a);
As a general rule of thumb, it is a bad idea to rely on currentTimeMillis() to have a millisecond-resolution on windows, especially if you are using it to generate unique things like file names or db IDs. Looks like System.nanoTime() might tick faster.

Here are some links I found on StackOverflow about this:

No comments:

Post a Comment

Please Provide your feedback here