• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unique value across JVMs

 
kundan varma
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
can some body tell me why this code will generate unique value across jvm

/** Unique value across JVMs on this machine. */
private static final int JVM_UNIQUE = (int) (System.currentTimeMillis() >>> 8);

Thanks
kundan
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I can tell, it's nonsense!
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's my thought too, but a quick search shows others who appear to do the same.

Dave
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David O'Meara:
That's my thought too, but a quick search shows others who appear to do the same.


How did you search for that?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried "System.currentTimeMillis() >>> 8" (with the quotes included) and got a lot of rubbish and a few valid hits.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
kundan varma
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks
kundan
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Good luck!
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
kundan varma
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all
i will try and again get back to you people for review
Thanks
kundan
 
Peter Hu
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Hu
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic