简体   繁体   中英

Generate auto increment number by using Java

I am trying to generate unique auto increment id which will started from 0 . I found something for doing this using below code:

private static final AtomicInteger count = new AtomicInteger(0);   
uniqueID = count.incrementAndGet(); 

I can create auto increment ID by manually :

For example :

int i = 0; // Obviously I will declare this variable as global
++i;

In this way I can manage to do this. But I don't want to do this manually. I am finding some java method which will take of all this stuff.

And I found below code :

private static final AtomicInteger count = new AtomicInteger(0);   
uniqueID = count.incrementAndGet(); 

Now my question is, Is this the right way to generate unique ID? Or, is there any other better solution?

如果它应该仅在单个进程中是唯一的,那么是的,您可以使用 AtomicInteger。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM