多线程获取有序随机数字(同步)

需求:
[quote]获取有序随机数字序列,在服务器上全局共享最新的序列.有序随机数字生成规则为 前N位变长递增唯一数字 + (后缀)固定M位随机数字. 例如: 1000 + 1234 -> 10001234; 1001 + 4321 -> 10014321; 1002 + 4567 -> 10024567[/quote]

测试类:
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class DigitServiceTest implements Runnable {

private DigitNumServiceInterface employeeService;

@Before
public void setUp() throws Exception {
}

@Test
public void digitNumTest() {
for (int i = 0; i < 15; i++)
new Thread(new DigitServiceTest()).start();
}

@After
public void tearDown() throws Exception {
}

public void run() {
employeeService = new EmployeeServiceImpl();
System.out.println(Thread.currentThread() + " empDigit->" + employeeService.getCurrentDigitNum());
}

@Test
public void normalTest() {
employeeService = new EmployeeServiceImpl();
System.out.println(Thread.currentThread() + " empDigit->" + employeeService.getCurrentDigitNum());
}
}

接口:
public interface DigitNumServiceInterface {
/**
* 如果系统中还没有数字帐号记录,以此为起始默认前缀.
* 数字帐号生成规则为前N位唯一数字 + DEFAULT_DIGIT_NUM_SUFFIX_LENGTH位随机数字
*/
Integer DEFAULT_DIGIT_NUM_START = 1000;
/**
* 数字帐号后缀随机数字长度.
*/
Integer DEFAULT_DIGIT_NUM_SUFFIX_LENGTH = 4;
/** 数字帐号初始长度 DEFAULT_DIGIT_NUM_SUFFIX_LENGTH + DEFAULT_DIGIT_NUM_START.length*/
Integer DEFAULT_DIGIT_NUM_MIN_LENGTH = 8;

/** 获取数据库中最新的数字帐号*/
String getLastDigitNumFromDB();

/**获取当前按规则生成好的数字帐号*/
String getCurrentDigitNum();

/** 根据最新的数字帐号获取下一个数字帐号的前缀*/
String getNextDigitNumPrefix(String lastDigitNum);
}

Mock类:
import com.srt.vas.lt.util.StringUtil;

public class EmployeeServiceImpl implements DigitNumServiceInterface {
static String CURRENT_DIGIT_NUM;

/**获取当前按规则生成好的数字帐号,生成失败返回null */
public String getCurrentDigitNum() {
synchronized (DigitNumServiceInterface.DEFAULT_DIGIT_NUM_SUFFIX_LENGTH) {//同步锁
String currentDigitNum = null;
if (CURRENT_DIGIT_NUM == null) {//查询当前系统最新数字帐号
System.out.println("isNull");
currentDigitNum = getLastDigitNumFromDB();
currentDigitNum = getNextDigitNumPrefix(currentDigitNum);
} else {
currentDigitNum = getNextDigitNumPrefix(CURRENT_DIGIT_NUM);
}
if (currentDigitNum != null)
currentDigitNum += StringUtil.randomNumeric(DEFAULT_DIGIT_NUM_SUFFIX_LENGTH);
// currentDigitNum += "0000";
CURRENT_DIGIT_NUM = currentDigitNum;
return CURRENT_DIGIT_NUM;
}
}

public String getNextDigitNumPrefix(String lastDigitNum) {
if (lastDigitNum == null)
return "" + (DEFAULT_DIGIT_NUM_START + 1);
if (lastDigitNum.length() < DEFAULT_DIGIT_NUM_MIN_LENGTH)
return null;
if (lastDigitNum.length() >= DEFAULT_DIGIT_NUM_MIN_LENGTH)
return ""
+ (Integer.parseInt(lastDigitNum.substring(0, lastDigitNum.length()
- DEFAULT_DIGIT_NUM_SUFFIX_LENGTH)) + 1);
return null;
}

public String getLastDigitNumFromDB() {
return "100100000";
}
}

[color=red]StringUtil[/color]:

import java.util.Collection;
import java.util.Map;
import java.util.Random;

import org.apache.commons.lang.RandomStringUtils;//apache common

public class StringUtil{
/**
* 判断字符是否为空,为null,是否由空白字符组成.
* @param str
* @return 为空,null,全部空白字符时返回true,否则false.
*/
public static boolean isEmpty(String str) {
return str == null || str.matches("^\\s*$");
}

/**
* 为null返回true.
* @see StringUtil#isEmpty(String)
* @param l
* @return
*/
public static boolean isEmpty(Long l) {
return l == null;
}

/**
* 为null 返回true.
* @param obj
* @return
*/
public static boolean isEmpty(Object obj) {
return obj == null;
}

@SuppressWarnings("unchecked")
public static boolean isEmpty(Collection c) {
return c == null || c.isEmpty();
}

/**
* 判断是否为空
* @param map
* @return
*/
@SuppressWarnings("unchecked")
public static boolean isEmpty(Map map) {
return map == null || map.isEmpty();
}

/**
* 返回Hql in子句风格的字符串. (Object[, Object])
* Exp: from DomesticCat cat where cat.name in ( 'Foo', 'Bar', 'Baz' )
* from DomesticCat cat where cat.name not in ( 'Foo', 'Bar', 'Baz' )
* @param collect
* @return 返回Hql in子句风格的字符串.
*/
public static String getHqlInStyle(Collection<Long> collect) {
Long[] a = collect.toArray(new Long[0]);
if (a.length == 0)
return "";

StringBuilder buf = new StringBuilder();
buf.append('(');
buf.append(a[0]);

for (int i = 1; i < a.length; i++) {
buf.append(", ");
buf.append(a[i]);
}

buf.append(")");
return buf.toString();
}

/**
* 生成指定长度的随机数字.
* @param count
* @return
*/
public static String randomNumeric(int count) {
return RandomStringUtils.randomNumeric(count);
}

/**
* 生成长度为1,值为1-9的随机数字.
* @return
*/
public static String randomNumericBetween1And9() {
return RandomStringUtils.random(1, "123456789");
}

/**
* 生成指定长度的随机数字,第一位不为0.
* @param count
* @return
*/
public static String randomNumericStartWithoutZero(int count) {
return StringUtil.randomNumericBetween1And9() + StringUtil.randomNumeric(count - 1);
}

/**
* 密码为6位随机数字,并且第一位大于0。
* @return
*/
public static String randomNumericPassword() {
return StringUtil.randomNumericBetween1And9() + StringUtil.randomNumeric(5);
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值