/**
* 高并发情况下获取订单号
*
* @author cst
*
*/
public final class OrderUtil {
private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
private static final AtomicInteger atomicInteger = new AtomicInteger(1000000);
/**
* 不连续的订单号
* 日期+UUID
* @param no
* 数据中心编号
* @return 唯一的、不连续订单号
*/
public static synchronized String getOrderNoByUUID() {
Integer uuidHashCode = UUID.randomUUID().toString().hashCode();
if (uuidHashCode < 0) {
uuidHashCode = uuidHashCode * (-1);
}
String date = simpleDateFormat.format(new Date());
return date + uuidHashCode;
}
/**
* 连续增长订单号
*
* AtomicInteger原子操作类
* @return 同一秒内订单连续的编号
*/
public static synchronized String getOrderNoByAtomic() {
atomicInteger.getAndIncrement();
int i = atomicInteger.get();
String date = simpleDateFormat.format(new Date());
return date + i;
}
}
高并发生成订单编号
最新推荐文章于 2022-08-15 10:55:36 发布