I'm working on a AEM project, and I have a component X. Along this component, I have another one which is a container of X. So, I can drag X instances and drop them onto the container.
To avoid problems between multiple instances of the component X, I assigned them an id, calling this Java function from the regarding X.jsp:
long id = System.currentTimeMillis();
Then, in the jsp, I have something similar to:
<div id='<%= id %>'>
</div>
Surprisingly for me, while inspecting the DOM with the Chrome Inspector, I have found a couple of repeated ids in my html structure. And of course, this caused a lot of problems.
I was able to fix this problem, by calling:
long id = System.nanoTime();
Am I going crazy and currentTimeMillis is returning repeated values? Is that possible?
Is that possible?
Absolutely. For a start, it's very possible for a computer to do more than one thing in a millisecond. For a second thing, the clock consulted here often won't have a granularity of milliseconds - you may find that it gives the same result for a while, then jumps by 10ms or 15ms.
You should definitely not use currentTimeMillis
for a unique ID. I wouldn't recommend using nanoTime
either, to be honest. Why not just use UUID
? That's what it's there for.
See more on this question at Stackoverflow