java - Generate ID fast and with high probability of uniqueness -


i want generate id event occur in application.

the event frequency user load, might occur hundreds-thousand of time per second.

i can't afford using uuid.randomuuid() because might problematic in performance matters - take @ this.

i thought of generating id follows:

system.currenttimemillis() + ";" + long.tostring(_random.nextlong()) 

when _random static java.util.random class holding.

my questions are:

  1. do think distribution of combination enough needs?
  2. does java's random implementation related current time , therefore fact i'm combining 2 dangerous?

i use following.

final atomiclong counter = new atomiclong(system.currenttimemillis() * 1000); 

and

long l = counter.getandincrement(); // takes less 10 nano-seconds of time. 

this unique within system , across restarts provided average less 1 million per second.

even @ rate, number not overflow time.

class main {     public static void main(string[] args) {         system.out.println(new java.util.date(long.max_value/1000));     } } 

prints

sun jan 10 04:00:54 gmt 294247 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -