juliet juliet - 1 year ago 79
Java Question

Delay by 1 millisecond not working?

I am trying to generate a number using:


I have to generate these numbers sometimes 5 times in a row, which happens so fast that they are the same (but I don't want them to be the same as we are using them as part of a unique field)

I thought I could put a delay in between when each one is generated, which would prevent them from being the same, using:


But this still generates the same number. It only seems to generate a new one if I increase it to about 60 and above. I am trying to understand why this is? Thanks

Answer Source

If you want to generate 5 numbers starting at the current time that aren't the same - which as far as I can tell is your only requirement - you can use

long t = System.currentTimeMillis();

long ts[] = { t, t+1, t+2, t+3, t+4 };

Thread.sleep is absurd here. The user (or whoever) should not experience a delay for something that can be computed now.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download