The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I tried that in google and spotted a couple shifting LEFT instead of RIGHT, and then adding in some random in case you generated two in the same millisecond. Shifting right loses digits which harms your chances for uniqueness.
Does your app have a database? You could just select a sequence number and always get a new value.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
To be specific, "System.currentTimeMillis() >>> 8" divides the millisecond value by 256, equivalent to dropping the milliseconds and dividing by 32, yielding a value of approximately the "current current half minute." This method is clearly likely to yield duplicate values, even between JVMs on different machines.
That last clause brings up something not mentioned in the original question: Does the value need to be unique across all JVMs running on any machine in the world (or even limited to a local area network)? If so, Google for "globally unique identifier," often abbreviated as GUID. There are several libraries that will generate them for you by combining various attributes to achieve maximum likelihood of randomness. Common attributes used include the current time, machine's IP address, a memory address of some object, some process's identifier, and a random number.
Joined: Mar 08, 2004
HI David and other THanks for your views.
"Does the value need to be unique across all JVMs running on any machine in the world (or even limited to a local area network)? "
I want it to be unique across all jvm running on any machine in the world.
Can you suggest me how to do this efficiently?
Joined: Aug 07, 2003
If you can use a prebuilt library, search for GUID and UUID. Universally unique sounds so much more unique than globally unique, don't you agree?
If you have to build your own, start with the ideas I presented and start diving into the JavaDocs! Even if this is the case, there's plenty of sample code that can get you started and keep you moving.
A GOOD GUID generator generates a GUID based on the machine MAC address and a timestamp at millisecond level or better, possibly together with a random number. MAC addresses are universally unique, so combining that with a timestamp gives you enough uniqueness unless on the same machine another GUID were to be generated inside the same millisecond.
Edit: if no MAC address is available (meaning the machine does not have any network card or other network hardware installed) usually a random number is used instead, possibly taking what machine data can be obtained (harddisk serial, CPU serial, etc.) as a basis. [ May 04, 2005: Message edited by: Jeroen Wenting ]
Joined: Mar 08, 2004
Thanks all i will try and again get back to you people for review Thanks kundan
By using purely code-generated GUID doesn't provide a satisfied answer. First, pure Java code won't get you the MAC address. Secondly, if the multiple JVMs run on the same machine, even MAC doesn't help. If your run-time environment is constrained by those mentioned above, your best bet is using a database.
Originally posted by Stefan Wagner: You don't need a database for UIDs.
Just a UID-Server with a counter, which stores it's next UID in a file to restore it after power-failure.
It will be faster to implement it, than to install a database.
Well, that depends. Most applications need a database. If your application requires one, wouldn't it be faster to use a sequence number object or just a table with a single record in a database?
If you have UID server developed already, it may be easy to just use it. But if you need to write from scratch, it woudn't be easier than using the database approach. To implement a UID server, for multiple JVMs on a single machine or even spread over multiple machines, you need to use a proper communication machanism. Socket is a good candidate. Besides, you need to handle the persistance of the sequence number to a file. You may also need to write code to monitor the health status of the server(checking to make sure it's running). In a corporate environment, you have to explain why you need the port for the UID server open, because it's subject to potential security violations. [ May 07, 2005: Message edited by: Paul HG ]
Joined: Jan 29, 2003
If you have a database, I'd use it. It's a great external source for numbers with solid concurrency management already in place. I use a vendor package that generated its own 40-some character GUIDs but our users need to see and use these keys now and then, so we wanted shorter ones. Now we go to the database for a number, then concatenate a locally maintained sequence of "n" digits. We hit the database at startup and every time the "n" digits roll over, so the db trips are not a performance problem.