---
title: "Id Generation for multiple forms using java"  
description: "Id Generation for multiple forms using java"  
author: "Anonymous User"  
published: 2013-10-14  
updated: 2013-10-14  
canonical: https://www.mindstick.com/forum/1657/id-generation-for-multiple-forms-using-java  
category: "java"  
tags: ["java"]  
reading_time: 1 minute  

---

# Id Generation for multiple forms using java

Can [anyone](https://www.mindstick.com/articles/23207/how-to-find-the-best-fit-job-for-anyone) suggest if i use below code to generate id for my [files](https://www.mindstick.com/articles/23302/the-importance-and-advantage-of-keeping-your-important-files-on-the-cloud), will it be [unique](https://www.mindstick.com/blog/12870/how-to-be-unique-in-business) always. As 100s [forms](https://www.mindstick.com/interview/34192/what-is-the-purpose-of-the-authentication-mode-forms-element-in-web-config) create the [form](https://www.mindstick.com/forum/6/multi-form-mfc-application) at same automatically which auto [populate](https://www.mindstick.com/blog/60/populate-records-in-second-listbox-according-to-the-selection-in-first-list-box) ids in ID textbox. So it should be thread [safe](https://www.mindstick.com/articles/126322/how-to-keep-your-home-safe-while-traveling) and If i restart the [application](https://www.mindstick.com/articles/12824/calculator-application-in-android) it should not ever [repeat](https://www.mindstick.com/forum/156775/give-an-example-of-the-linq-repeat-method) the id which [already](https://yourviews.mindstick.com/view/88453/the-hidden-ways-ai-is-already-controlling-your-daily-life) 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);
    try
    {
    Thread.sleep(1);
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    return val;
} 
```

And forms are getting the Id using ClassName.generateIdforFile();

## Replies

### Reply by Hank Greenberg

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;
}
```

\


---

Original Source: https://www.mindstick.com/forum/1657/id-generation-for-multiple-forms-using-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
