aspose file tools*
The moose likes Java in General and the fly likes Unique value across JVMs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Unique value across JVMs " Watch "Unique value across JVMs " New topic
Author

Unique value across JVMs

kundan varma
Ranch Hand

Joined: Mar 08, 2004
Posts: 322
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


SCJP1.4,SCBCD,SCEA,CNA
Failures are practice shoots for success.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
As far as I can tell, it's nonsense!


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
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

That's my thought too, but a quick search shows others who appear to do the same.

Dave
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Jan 29, 2003
Posts: 8791
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
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Mar 08, 2004
Posts: 322
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

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Oct 12, 2000
Posts: 5093
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 ]

42
kundan varma
Ranch Hand

Joined: Mar 08, 2004
Posts: 322
Thanks all
i will try and again get back to you people for review
Thanks
kundan
Peter Hu
Ranch Hand

Joined: Apr 15, 2005
Posts: 33
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

Joined: Jun 02, 2003
Posts: 1923

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.


http://home.arcor.de/hirnstrom/bewerbung
Peter Hu
Ranch Hand

Joined: Apr 15, 2005
Posts: 33
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

Joined: Jan 29, 2003
Posts: 8791
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.
Sean Sullivan
Ranch Hand

Joined: Sep 09, 2001
Posts: 427
http://wiki.java.net/bin/view/Javapedia/UUID

http://java.sun.com/j2se/1.5.0/docs/api/java/util/UUID.html

http://jakarta.apache.org/commons/sandbox/id/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unique value across JVMs