Home > DeveloperSection > Forums > Id Generation for multiple forms using java
Ankit Singh

Total Post:341

Posted on    October-14-2013 3:33 AM

 Java Java 

 1 Reply(s)
 698  View(s)
Rate this:

Can anyone suggest if i use below code to generate id for my files, will it be unique always. As 100s forms create the form at same automatically which auto populate ids in ID textbox. So it should be thread safe and If i restart the application it should not ever repeat the id which already generated before the application stop anytime.

public static synchronized String generateIdforFile()  
     AtomicLong counter = new AtomicLong(System.currentTimeMillis()*1000);  
         String val=Long.toString(counter.incrementAndGet(), 36);  
    catch (Exception e)   
    return val;  

And forms are getting the Id using ClassName.generateIdforFile();

Hank Greenberg
Hank Greenberg

Total Post:12

Posted on    October-14-2013 3:52 AM

Better use a global AtomicLong starting at 0L for you entire application. Then you concatenate with CurrentTimeMillis.

static AtomicLong counter = new AtomicLong(0L);
public static String generateIdforFile()  
    String timeString = Long.toString(System.currentTimeMillis(), 36);
    String counterString = Long.toString(counter.incrementAndGet() % 1000, 36);
    return timeString + counterString;

Modified On Oct-14-2013 03:54:41 AM

Don't want to miss updates? Please click the below button!

Follow MindStick