有事没事领个红包

用户ID的代码生成

public class Uid {

    private static final String machineIdString = Integer.toHexString(new Object().hashCode());
    private static final AtomicInteger globalCount = new AtomicInteger(0);

    private final int count = globalCount.incrementAndGet();
    private final long time = System.currentTimeMillis();

    /**
     * Generates a new identifier.
     */
    private Uid() {
        super();
    }

    /**
     * Generates a new identifier String.
     */
    public static String generateUidString() {
        String idString = new Uid().toString();
        return idString;
    }

    /**
     * @see Object#toString()
     */
    public String toString() {
        String timeString = Long.toHexString(time);
        String countString = Integer.toHexString(count);

        return machineIdString + "_" + timeString + "_" + countString;
    }

}

 

posted @ 2017-07-05 17:12  crazyCodeLove  阅读(466)  评论(0编辑  收藏  举报