package com.jeeplus.modules.biz.util;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
/**
* 单例模式
* 生成订单编号
*/
public class SingletonUtil {
private volatile static SingletonUtil singletonUtil;
private SingletonUtil(){}
public static SingletonUtil getSingletonUtil(){
if (singletonUtil == null){
synchronized (SingletonUtil.class){
if(singletonUtil==null){
singletonUtil = new SingletonUtil();
}
}
}
return singletonUtil;
}
/**
* 生成编号
* @param prefix
* @return
*/
public String makeOrderNum(String prefix){
SimpleDateFormat simpleDateFormat;
simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date();
String str =prefix + "-" + simpleDateFormat.format(date);
Random random = new Random();
int ranNum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;// 获取5位随机数
System.out.println("生成订单号:"+str + ranNum);
return str + ranNum;// 当前时间
}
/**
* 测试
* @param args
*/
public static void main(String[] args) {
// 测试多线程调用订单号生成工具
try {
for (int i = 0; i < 100; i++) {
Thread t1 = new Thread(new Runnable() {
public void run() {
SingletonUtil makeOrder = new SingletonUtil();
String prefix="HD";
makeOrder.makeOrderNum(prefix);
}
}, "at" + i);
t1.start();
Thread t2 = new Thread(new Runnable() {
public void run() {
SingletonUtil makeOrder = new SingletonUtil();
String prefix="AB";
makeOrder.makeOrderNum(prefix);
}
}, "bt" + i);
t2.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}