Util--RandNumGenerator

开发者博客:[url]http://www.developsearch.com[/url]

/**
* 19位长度随机数生成器,生成规则为当前系统时间(13位长度的) + 毫秒级别的 自增数字。
*
* @author chenxin
* @version [版本号, 2012-5-21]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class RandNumGenerator {

static AtomicInteger ai = new AtomicInteger(10000);

/**
* uuid分隔符
*/
private static final String UUID_SP = "-";

/**
* uuid替换符
*/
private static final String UUID_RP = "";

/**
* 私有构造
*/
private RandNumGenerator() {
}

/**
* 同步的生成新的随机数的方法
*
* @return 19位长度随机数
*/
public static long nextNumber() {
long curTime = System.currentTimeMillis() * 100000L;
// 如果本次随机数产生时间与上一次产生时间在毫秒级别
// 是一样的,则自增数++,否则自增序列归0
return curTime + nextCountValue();
}

/**
* 获取6位长度序列自增数
*
* @return
*/
private static long nextCountValue() {
ai.compareAndSet(100000, 10000);
return ai.getAndAdd(1);
}

/**
* 生成一个UUID
* @return UUID
*/
public static String nextUUID(){
String s = UUID.randomUUID().toString();//
return s.replaceAll(UUID_SP, UUID_RP);
}

public static void main(String[] args){
long start = System.currentTimeMillis();
for (int i = 0; i < 10000000; i++) {
nextNumber();
//System.out.println(nextNumber());
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值