sleep and current Time

Hi, i'm involved in a routine to get uids based on time that looks like that..

long timeInMillis;

timeInMillis = System.currentTimeMillis();// A

try{

Thread.sleep(30);

}catch (InterruptedException e){

e.printStackTrace();

}

timeInMillis = System.currentTimeMillis();// B

the problem is that value of timeInMillis is the same on A and B...

( and no exception is thrown )

[706 byte] By [pedroerenciaa] at [2007-11-27 2:09:49]
# 1

> the problem is that value of timeInMillis is the same

> on A and B... ( and no exception is thrown )

First of all, the timer resolution on your OS or hardware or JVM is most likely

NOT precise enough to have every single millisecond be distinct.

Second of all, sleep() will only sleep roughly that amount of time.

JVM and the OS cannot guarantee the amount of time.

And most important of all, DO NOT USE time as the basis for a Unique ID!!!

Use an appropriate API for your database application or use a persistent global counter.

KathyMcDonnella at 2007-7-12 2:00:50 > top of Java-index,Java Essentials,Java Programming...
# 2
Hi i need to know about the Unique Id generation... do u knw the specfic class or API in java.........
83Krisha at 2007-7-12 2:00:51 > top of Java-index,Java Essentials,Java Programming...
# 3

It depends on what you are using it for.

After all, an unique ID is just an ID that you haven't used before.

The simplest way is to have a global integer "id=0".

So, the first ID is 0. The next ID is 1. The next ID is 2...

If your application is using a database, you should use

the database's unique ID generation. All databases have it,

since they need it for table keys and stuff.

If your application is security, then you don't want to use 1, 2, 3...

since people can guess what the IDs are. You will want to apply

some salted hashing or other cryptographic transformations.

.......

It depends on what you are using it for.

KathyMcDonnella at 2007-7-12 2:00:51 > top of Java-index,Java Essentials,Java Programming...