前两天有人提过,按照时间(带MILLISECOND E.g., at 10:04:15.250 PM the MILLISECOND is 250.)基本上就是唯一的!

解决方案 »

  1.   

    我这里有一个程序可以生成UUID,是从一个C程序移植过来的,原来是根据网卡号和系统时间戳生成,但Java里面没法读取网卡号,就改为用IP地址。import java.net.*;
    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);
        }
    }
      

  2.   

    // 构造一个128位的整数表示一个极大范围的唯一标识
    这个地方的思路是如何的?!~~
    也就是如何将系统时间+IP来生成唯一ID?!~~