Jump to Real's How-to Main page

Use System time to generate unique ID

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++;
    }
}

See also this HowTo


If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2005
[ home ]