以秒为单位生成唯一的时间序列号
- - ITeye博客//测试是否有生成重复的ID. private static final byte LEVEL = 7; //限定一秒钟最多产生1000万-1 个数. * 测试机器系统参数: Win7 64位 i5-4210M 4core 2.6GHz 内存8GB. * 测试10个线程并发产生,每秒可以产生310万左右个序列号.
/**
* 功能: 以秒为单位生成唯一的序列号 <br/>
* 生成格式:YYMMddHHmmssXXXXXXX <br/>
* XXXXXXX:代表序列号,从1开始
* 例子: 1808312321280000001 或者 1808312321280001234 <br/>
* <p>局限性: 每秒生成最大范围 (1000万-1) 个数</p>
* Created by Administrator on 2018/8/31.
*/
public final class GenerateDateTimeUniqueID {
private static final DateFormat DF = new SimpleDateFormat("yyMMddHHmmss");
private static volatile long LAST_TIME = -1;
private static final AtomicInteger COUNT = new AtomicInteger();
private static final Object LOCK = new Object();
//测试是否有生成重复的ID
public static final ConcurrentMap<String, Boolean> MAP = new ConcurrentHashMap<String, Boolean>();
//1 000 000
private static final byte LEVEL = 7; //限定一秒钟最多产生1000万-1 个数
private static final String ZERO_0 = "";
private static final String ZERO_1 = "0";
private static final String ZERO_2 = "00";
private static final String ZERO_3 = "000";
private static final String ZERO_4 = "0000";
private static final String ZERO_5 = "00000";
private static final String ZERO_6 = "000000";
/*******
*
* 测试机器系统参数: Win7 64位 i5-4210M 4core 2.6GHz 内存8GB
*
* ********/
/**
* 测试10个线程并发产生,每秒可以产生310万左右个序列号
* *
*/
public static String generateDateTimeUniqueIdStr() throws Exception {
Date date = new Date();
String dateStr = DF.format(date);
long curTime = Long.parseLong(dateStr);
int curCount = 0;
synchronized (GenerateDateTimeUniqueID.class) {
if (curTime < LAST_TIME) {
curTime = LAST_TIME;
} else if (curTime > LAST_TIME) {
LAST_TIME = curTime;
System.out.println(Thread.currentThread().getName() + "-" + COUNT.get());
COUNT.set(0);
}
curCount = COUNT.incrementAndGet();
}
return curTime + format(curCount);
}
/**
* 测试10个线程并发产生,每秒可以产生290万左右个序列号
* *
*/
public static long generateDateTimeUniqueId() {
Date date = new Date();
String dateStr = DF.format(date);
long curTime = Long.parseLong(dateStr);
int curCount = 0;
synchronized (GenerateDateTimeUniqueID.class) {
if (curTime < LAST_TIME) {
curTime = LAST_TIME;
} else if (curTime > LAST_TIME) {
LAST_TIME = curTime;
System.out.println(Thread.currentThread().getName() + "-" + COUNT.get());
COUNT.set(0);
}
curCount = COUNT.incrementAndGet();
}
return Long.parseLong(curTime + format(curCount));
}
private static String format(int value) {
int temp = value;
int count = 0;
while (temp > 0) {
temp = temp / 10;
count++;
}
switch (LEVEL - count) {
case 0:
return ZERO_0 + value;
case 1:
return ZERO_1 + value;
case 2:
return ZERO_2 + value;
case 3:
return ZERO_3 + value;
case 4:
return ZERO_4 + value;
case 5:
return ZERO_5 + value;
case 6:
return ZERO_6 + value;
default:
return "9999999";
// throw new IllegalStateException(); //再处理
}
}
private GenerateDateTimeUniqueID() {
}
public static void main(String[] args) {
long cur = System.currentTimeMillis();
for (int i = 1; i < 1000000000; i++) {
System.out.println(generateDateTimeUniqueId());
}
// int num = 6;
// ExecutorService executorService = Executors.newFixedThreadPool(num);
// for (int i = 0; i < num; i++) {
// executorService.submit(new TestThread());
// }
System.out.println("spend " + (System.currentTimeMillis() - cur) + " ms");
}
static class TestThread implements Runnable {
@Override
public void run() {
while (true) {
String id = generateDateTimeUniqueIdStr();
Boolean isHas = MAP.putIfAbsent(id, Boolean.TRUE);
if (isHas != null) {
throw new IllegalArgumentException();
}
}
}
}
}