2007/01/07 13:13 Developer
자바로 UID만들기 : 세상에서 유일한 값(unique value)
가끔 코딩을 하다가 보면 unique한 값을 사용해야 할때가 있다.
여러가지 조합으로 만들기도 하지만, 자바 자체의 API를 이용해서 좀더 편하게 만들수 있을 것이다.
A UID represents an identifier that is unique over time with respect to the host it is generated on, or one of 216 "well-known" identifiers.
unique, an int that uniquely identifies the VM that this UID was generated in, with respect to its host and at the time represented by the time value (an example implementation of the unique value would be a process identifier), or zero for a well-known UID
time, a long equal to a time (as returned by System.currentTimeMillis()) at which the VM that this UID was generated in was alive, or zero for a well-known UID
count, a short to distinguish UIDs generated in the same VM with the same time value http://java.sun.com/j2se/1.5.0/docs/api/java/rmi/server/UID.html
VMID is a identifier that is unique across all Java virtual machines. VMIDs are used by the distributed garbage collector to identify client VMs.
VMID()
Create a new VMID. Each new VMID returned from this constructor is unique for all Java virtual machines under the following conditions: a) the conditions for uniqueness for objects of the class
java.rmi.server.UID are satisfied, and b) an address can be obtained for this host that is unique and constant for the lifetime of this object. http://java.sun.com/j2se/1.5.0/docs/api/java/rmi/dgc/VMID.html
분산환경에서 여러개의 VM이 존재를 한다면, UID보다는 VMID를 추천하고 싶다.
샘플 코드르 작성해봤다.
이건 실행결과다. 원하는 필드의 사이즈에 맞게 적절하게 사용하면 되겠다.