前两天有人提过,按照时间(带MILLISECOND E.g., at 10:04:15.250 PM the MILLISECOND is 250.)基本上就是唯一的!
解决方案 »
- 在JAVA技术中wait(),notifyAll(),sleep(),yield(),synchronized(this)哪个方法可以在任何时候被任任何线程
- 这个问题,怎么解决?寻求高手~~~~~~
- 我想做这么个东西,麻烦谁能给个思路谢谢
- 用JAVA打开一个IE窗口,打开时,怎么控制窗口的大小?高分求救!
- 一个MainFrame,左边是JTree, 右边是JTable。TreeSelectionListener单独成类,JTree和JTable的实现不在MainFrame中,要实现根据JTree的
- protected权限问题:子类竟然不能创建父类对象调用父类的protected方法!!??
- 在排序列表中,快速插入,不能全部重新排序。
- jbuilder9中代码与设计不同步的问题,怎么回事?
- 我的电脑运行不了Javac命令,求救!
- 偶想在 swing界面 上画一个' S 型 '的线,最好是3D效果的!
- 今天把JB的JDK升级到了1。4。0,结果原来程序中的中文都显示成了方框,怎么解决?经验高手指点了。。。
- java本地化的问题(各位帮帮我!)
import java.io.*;public class UUID
{
private long high, low; private static int UUID_HOST_LOCK_PORT = 7750;
private static final int MAX_RETRYS = 1200;
private static final int INTERVAL_TIME = 100;
private static ServerSocket lockSocket; private static long timeStamp;
private static long adapterAddress;
private static int instanceCounter; private static final long versionMask = 0x1000L;
private static final long reserveMask = 0xe000000000000000L;
private static final long randomMask = 0x1fffffffL; /**
* 不允许直接构造UUID对象
*/
private UUID() {} /**
* 私有构造方法
*/
private UUID(long high, long low) {
this.high = high;
this.low = low;
} /**
* 生成唯一标识符
*/
public static synchronized UUID create()
throws UUIDException
{
if (timeStamp == 0L)
setTimeStamp(); // 初始化时间戳
if (adapterAddress == 0L)
setAdapterAddress(); // 初始化网卡地址(实际为IP地址) // 构造一个128位的整数表示一个极大范围的唯一标识
UUID uuid = new UUID();
long midTime = (timeStamp >> 32) & 0xffffffffL;
uuid.high = (timeStamp << 32) |
((midTime << 16) & 0xffff0000L) |
versionMask |
((timeStamp >> 48) & 0x0fffL); int count = instanceCounter++;
if (count == 0x1fffffff) {
instanceCounter = 0;
setTimeStamp();
}
uuid.low = ((count & randomMask) << 32) |
reserveMask |
adapterAddress; return uuid;
} private static void setAdapterAddress()
throws UUIDException
{
try {
byte addr[] = InetAddress.getLocalHost().getAddress();
int raw = (addr[3] & 0x000000ff) |
((addr[2] << 8) & 0x0000ff00) |
((addr[1] << 16) & 0x00ff0000) |
((addr[0] << 24) & 0xff000000);
adapterAddress = (long)raw & 0xffffffffL;
} catch (UnknownHostException e) {
throw new UUIDException("Unexpected failure");
}
} private static void setTimeStamp()
throws UUIDException
{
acquireHostLock();
try {
long newTime = System.currentTimeMillis();
if (timeStamp != 0L) {
if (newTime < timeStamp) {
throw new UUIDException("Unique identifier clock failure");
}
if (newTime == timeStamp) {
letClockTick(newTime);
newTime = System.currentTimeMillis();
}
}
timeStamp = newTime;
} finally {
releaseHostLock();
}
} private static void letClockTick(long curTime)
throws UUIDException
{
int timeoutCounter = 0;
long sleepTime = 1L; for (long newTime = System.currentTimeMillis();
newTime == curTime; newTime = System.currentTimeMillis()) {
timeoutCounter++;
sleepTime *= 2;
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
// ignore
}
if (sleepTime > 60000) // 60 seconds
throw new UUIDException("Unique identifier unexpected failure");
}
} private static void acquireHostLock()
throws UUIDException
{
int numberOfRetrys = 0;
while (lockSocket == null) {
try {
lockSocket = new ServerSocket(UUID_HOST_LOCK_PORT);
return;
} catch (BindException e) {
try {
Thread.sleep(INTERVAL_TIME);
} catch (InterruptedException e1) {
// ignore
}
} catch (IOException e2) {
throw new UUIDException("Unique identifier unexpected failure");
}
if (numberOfRetrys == MAX_RETRYS)
throw new UUIDException("Unique identifier lock failure");
numberOfRetrys++;
}
} private static void releaseHostLock() {
if (lockSocket != null) {
try {
lockSocket.close();
} catch (IOException e) {
// ingore
}
lockSocket = null;
}
} public boolean equals(Object obj) {
if (this == obj)
return true; if (!(obj instanceof UUID))
return false; UUID other = (UUID)obj;
return this.high == other.high && this.low == other.low;
} public int hashCode() {
return (int)(low << 24) & 0xff000000 |
(int)(high >> 20) & 0x00fff000 |
(int)(low >> 32) & 0x00000fff;
} public String toString() {
StringBuffer buf = new StringBuffer();
buf.append(intToHexString((int)(high >>> 32))).append("-");
buf.append(shortToHexString((short)(high >>> 16))).append("-");
buf.append(shortToHexString((short)high)).append("-");
buf.append(shortToHexString((short)(low >>> 48))).append("-");
buf.append(shortToHexString((short)(low >>> 32)));
buf.append(intToHexString((int)low));
return buf.toString();
} private static final char[] hexDigits = {
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
}; private static String intToHexString(int n) {
char[] buf = {
hexDigits[(n >>> 28) & 0x0F],
hexDigits[(n >>> 24) & 0x0F],
hexDigits[(n >>> 20) & 0x0F],
hexDigits[(n >>> 16) & 0x0F],
hexDigits[(n >>> 12) & 0x0F],
hexDigits[(n >>> 8) & 0x0F],
hexDigits[(n >>> 4) & 0x0F],
hexDigits[(n ) & 0x0F]
};
return new String(buf);
} public static String shortToHexString(short n) {
char[] buf = {
hexDigits[(n >>> 12) & 0x0F],
hexDigits[(n >>> 8) & 0x0F],
hexDigits[(n >>> 4) & 0x0F],
hexDigits[(n ) & 0x0F]
};
return new String(buf);
} public static void main(String args[]) {
UUID uuid = UUID.create();
System.out.println(uuid.toString());
}
}
public class UUIDException extends RuntimeException
{
public UUIDException() {
super();
} public UUIDException(String msg) {
super(msg);
}
}
这个地方的思路是如何的?!~~
也就是如何将系统时间+IP来生成唯一ID?!~~