UUID(Universally Unique Identifier)全局唯一标识符,是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的。按照开放软件基金会(OSF)制定的标准计算,用到了以太网卡地址、纳秒级时间、芯片ID码和许多可能的数字。由以下几部分的组合:当前日期和时间(UUID的第一个部分与时间有关,如果你在生成一个UUID之后,过几秒又生成一个UUID,则第一个部分不同,其余相同),时钟序列,全局唯一的IEEE机器识别号(如果有网卡,从网卡获得,没有网卡以其他方式获得),UUID的唯一缺陷在于生成的结果串会比较长。
在Java中生成UUID主要有以下几种方式:
1. JDK1.5
如果使用的JDK1.5的话,那么生成UUID变成了一件简单的事,以为JDK实现了UUID: java.util.UUID,直接调用即可.
2. 第三方开源类库(推荐使用):
最著名的是 JUG .特点上是: 纯Java实现,开源,LGPL协议。采用了Native的方式产生真正的Uuid.而且提供了不同平台的实现,包括:
3. Java代码实现:
如果你使用JDK1.4或以前版本,又不想加入第三方的类库的话,下面提供了一个纯Java的UUID实现. 不过需要注意的是:这里产生的可能不是真正的UUID,只不过重复的机会少一些而已。
其中使用到MD5加密算法,实现代码如下:
在Java中生成UUID主要有以下几种方式:
1. JDK1.5
如果使用的JDK1.5的话,那么生成UUID变成了一件简单的事,以为JDK实现了UUID: java.util.UUID,直接调用即可.
UUID uuid = UUID.randomUUID();
2. 第三方开源类库(推荐使用):
最著名的是 JUG .特点上是: 纯Java实现,开源,LGPL协议。采用了Native的方式产生真正的Uuid.而且提供了不同平台的实现,包括:
- Linux / x86
- Windows (98, ME, NT, 2K, XP?) / x86
- Solaris / Sparc
- Mac OS X
- FreeBSD / x86
import
org.doomdark.uuid.UUID;
import org.doomdark.uuid.UUIDGenerator;
UUIDGenerator generator = UUIDGenerator.getInstance();
UUID uuid = generator.generateRandomBasedUUID();
import org.doomdark.uuid.UUIDGenerator;
UUIDGenerator generator = UUIDGenerator.getInstance();
UUID uuid = generator.generateRandomBasedUUID();
3. Java代码实现:
如果你使用JDK1.4或以前版本,又不想加入第三方的类库的话,下面提供了一个纯Java的UUID实现. 不过需要注意的是:这里产生的可能不是真正的UUID,只不过重复的机会少一些而已。
import
java.net.InetAddress;
import java.net.UnknownHostException;
/** */ /**
* UUID(Univeral Unique Identity)类
*
*
*
*
*
* @version 1.0
*/
public final class UUID {
/** *//**
* @serial Integer that helps create a unique UID.
*/
private int unique;
/** *//**
* @serial Long used to record the time. The
* used to create a unique UID.
*/
private long time;
/** *//**
* InetAddress to make the UID globally unique
*/
private static String address;
/** *//**
* a random number
*/
private static int hostUnique;
/** *//**
* Used for synchronization
*/
private static Object mutex;
private static long lastTime;
private static long DELAY;
private static String generateNoNetworkID() {
Thread current = Thread.currentThread();
String nid = current.activeCount() + System.getProperty("os.version")
+ System.getProperty("user.name ")
+ System.getProperty("java.version");
System.out.println("netWorkId =" + nid);
MD5 md5 = new MD5(nid);
md5.processString();
return md5.getStringDigest();
}
static {
hostUnique = (new Object()).hashCode();
mutex = new Object();
lastTime = System.currentTimeMillis();
DELAY = 10; // in milliseconds
try {
String s = InetAddress.getLocalHost().getHostAddress();
MD5 md5 = new MD5(s);
md5.processString();
address = md5.getStringDigest();
} catch (UnknownHostException ex) {
address = generateNoNetworkID();
}
}
public UUID() {
synchronized (mutex) {
boolean done = false;
while (!done) {
time = System.currentTimeMillis();
if (time lastTime + DELAY) {
// pause for a second to wait for time to change
try {
Thread.currentThread().sleep(DELAY);
} catch (java.lang.InterruptedException e) {
} // ignore exception
continue;
} else {
lastTime = time;
done = true;
}
}
unique = hostUnique;
}
}
public String toString() {
return Integer.toString(unique, 16) + Long.toString(time, 16) + address;
}
public boolean equals(Object obj) {
if ((obj != null) && (obj instanceof UUID)) {
UUID uuid = (UUID) obj;
return (unique == uuid.unique && time == uuid.time && address
.equals(uuid.address));
} else {
return false;
}
}
public static void main(String args[]) {
System.out.println(new UUID());
System.out.println(new UUID());
System.out.println(new UUID());
long start = System.currentTimeMillis();
System.out.println(new UUID());
long end = System.currentTimeMillis();
System.out.println((end - start));
System.out.println(new UUID().toString().length());
}
/** *//**
* 返回最新的UUID号码
*
* @return String UUID,长50位
*
*/
public final static String getUUID() {
UUID uid = new UUID();
return uid.toString();
}
}
import java.net.UnknownHostException;
/** */ /**
* UUID(Univeral Unique Identity)类
*
* Title: 生成唯一编码
*
*
* Description: 源自于w3c.org <</font>
*
*
* Copyright: Copyright (c) 2001-2004
*
*
* @version 1.0
*/
public final class UUID {
/** *//**
* @serial Integer that helps create a unique UID.
*/
private int unique;
/** *//**
* @serial Long used to record the time. The
time
will be* used to create a unique UID.
*/
private long time;
/** *//**
* InetAddress to make the UID globally unique
*/
private static String address;
/** *//**
* a random number
*/
private static int hostUnique;
/** *//**
* Used for synchronization
*/
private static Object mutex;
private static long lastTime;
private static long DELAY;
private static String generateNoNetworkID() {
Thread current = Thread.currentThread();
String nid = current.activeCount() + System.getProperty("os.version")
+ System.getProperty("user.name ")
+ System.getProperty("java.version");
System.out.println("netWorkId =" + nid);
MD5 md5 = new MD5(nid);
md5.processString();
return md5.getStringDigest();
}
static {
hostUnique = (new Object()).hashCode();
mutex = new Object();
lastTime = System.currentTimeMillis();
DELAY = 10; // in milliseconds
try {
String s = InetAddress.getLocalHost().getHostAddress();
MD5 md5 = new MD5(s);
md5.processString();
address = md5.getStringDigest();
} catch (UnknownHostException ex) {
address = generateNoNetworkID();
}
}
public UUID() {
synchronized (mutex) {
boolean done = false;
while (!done) {
time = System.currentTimeMillis();
if (time lastTime + DELAY) {
// pause for a second to wait for time to change
try {
Thread.currentThread().sleep(DELAY);
} catch (java.lang.InterruptedException e) {
} // ignore exception
continue;
} else {
lastTime = time;
done = true;
}
}
unique = hostUnique;
}
}
public String toString() {
return Integer.toString(unique, 16) + Long.toString(time, 16) + address;
}
public boolean equals(Object obj) {
if ((obj != null) && (obj instanceof UUID)) {
UUID uuid = (UUID) obj;
return (unique == uuid.unique && time == uuid.time && address
.equals(uuid.address));
} else {
return false;
}
}
public static void main(String args[]) {
System.out.println(new UUID());
System.out.println(new UUID());
System.out.println(new UUID());
long start = System.currentTimeMillis();
System.out.println(new UUID());
long end = System.currentTimeMillis();
System.out.println((end - start));
System.out.println(new UUID().toString().length());
}
/** *//**
* 返回最新的UUID号码
*
* @return String UUID,长50位
*
*/
public final static String getUUID() {
UUID uid = new UUID();
return uid.toString();
}
}
其中使用到MD5加密算法,实现代码如下: