Generate auto increment number by using Java -


i trying generate unique auto increment id started 0. found doing using below code:

private static final atomicinteger count = new atomicinteger(0);    uniqueid = count.incrementandget();  

i can create auto increment id manually :

for example :

int = 0; // declare variable global ++i; 

in way can manage this. don't want manually. finding java method take of stuff.

and found below code :

private static final atomicinteger count = new atomicinteger(0);    uniqueid = count.incrementandget();  

now question is, right way generate unique id? or, there other better solution?

if should unique within single process, yes can use atomicinteger.


Comments