randomsecquencecreator.java

来自「cwbbs 云网论坛源码」· Java 代码 · 共 69 行

JAVA
69
字号
package cn.js.fan.util;import java.util.Random;import java.util.Hashtable;public class RandomSecquenceCreator {    static Hashtable hash = new Hashtable();        static Random rand = new Random(System.currentTimeMillis());    static long lastRandTime = System.currentTimeMillis();    public RandomSecquenceCreator() {    }        public static String getId() {                if (System.currentTimeMillis()-lastRandTime>20000)            hash.clear();        Integer id = new Integer(0);        synchronized (hash) {                        id = new Integer(rand.nextInt());            while (hash.containsKey(id)) {                id = new Integer(rand.nextInt());            }                        String data = "";            hash.put(id, data);        }        lastRandTime = System.currentTimeMillis();        return System.currentTimeMillis() + "" + Math.abs(id.intValue());    }        public static String getId(int length) {                if (System.currentTimeMillis()-lastRandTime>20000)            hash.clear();        Integer id = new Integer(0);        String strId = "";        synchronized (hash) {                        id = new Integer(rand.nextInt());            if (length > 15)                strId = System.currentTimeMillis() + "" + Math.abs(id.intValue());            else                strId = "" + Math.abs(id.intValue());            if (strId.length() > length)                strId = strId.substring(0, length);            while (hash.containsKey(strId)) {                id = new Integer(rand.nextInt());                if (length > 15)                    strId = System.currentTimeMillis() + "" + Math.abs(id.intValue());                else                    strId = "" + Math.abs(id.intValue());                if (strId.length() > length)                    strId = strId.substring(0, length);            }                        String data = "";            hash.put(strId, data);        }        lastRandTime = System.currentTimeMillis();        return strId;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?