Tuesday, August 19, 2008

Use System time to generate unique Number with JAVA

Since the granulaty of a PC can be as high as 55ms (down to 10ms), you can't use the System time to generate a unique ID because of the risk of getting duplicated IDs. This can be solved by using the following technique to make sure that the number returned is unique (in a single JVM).

public class UniqueID {
static long current= System.currentTimeMillis();
static public synchronized long get(){
return current++;
}
}

No comments:

search engine

Custom Search