Link to home
Start Free TrialLog in
Avatar of hasozduru
hasozduru

asked on

Generate unique id

Hi

How can I generate unique id?

Thanks
Avatar of fargo
fargo

there are many ways of doing it, in my opinion following is the best (orderwise)
* use sequences in database
OR
* may be use like String id =  new Long(System.currentTimeMillis()).toString()
second option can be better replaced with the following
http://www.rgagnon.com/javadetails/java-0385.html
Avatar of rrz
How are you going to use the id ?  Are you naming a file ? If yes, maybe
https://www.experts-exchange.com/questions/21833607/Using-createTempFile-method.html 
will help you.  You could  just use System.currentTimeMillis() as discussed at the link or you could generate a random number and it would probably be unique every time.     rrz
sorry fargo I didn't see your posts.  
Avatar of hasozduru

ASKER

But there is possibility to create two same id's with millisecond, isn't there?
u need to make it synchronised then. I mean have a method which returns unique id and make it synchronised.
What do you mean? Can you please give me the code to do that?
ASKER CERTIFIED SOLUTION
Avatar of fargo
fargo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial