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:
- do think distribution of combination enough needs?
- 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
Post a Comment