java 生成 序列号 随机字符串等
2014-10-23 11:40
417 查看
生成序列号尤其要注意的是高并发时候序列号重复和抢占带来的唯一ID问题:
使用的时候可以与时间以及其他业务编号结合使用保证序列的唯一性:比如生成订单号,流水号等
方便随手使用
原文链接 http://blog.csdn.net/zl378837964/article/details/40394459
private static int maxvaluefive=99999999; private static int minvaluefive=0; private static AtomicInteger atomic = new AtomicInteger(minvaluefive); /** 生成序列号 */ static String getSeqFive(int coverPad) { for (;;) { int current = atomic.get(); int newValue = current >= maxvaluefive ? minvaluefive : current + 1; if (atomic.compareAndSet(current, newValue)) { return StringUtils.leftPad(String.valueOf(current), coverPad, "0"); } } }
使用的时候可以与时间以及其他业务编号结合使用保证序列的唯一性:比如生成订单号,流水号等
private static Random strGen = new Random();; private static Random numGen = new Random();; private static char[] numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();; private static char[] numbers = ("0123456789").toCharArray();; /** * 产生随机字符串 * */ public static final String randomString(int length) { if (length < 1) { return null; } char[] randBuffer = new char[length]; for (int i = 0; i < randBuffer.length; i++) { randBuffer[i] = numbersAndLetters[strGen.nextInt(61)]; } return new String(randBuffer); } /** * 产生随机数值字符串 * */ public static final String randomNumStr(int length) { if (length < 1) { return null; } char[] randBuffer = new char[length]; for (int i = 0; i < randBuffer.length; i++) { randBuffer[i] = numbers[numGen.nextInt(9)]; } return new String(randBuffer); }
方便随手使用
原文链接 http://blog.csdn.net/zl378837964/article/details/40394459
相关文章推荐
- java 生成一个随机字符串
- Java生成随机字符串,必须包含数字、小写字母、大写字母
- java生成随机字符串
- Java生成随机字符串
- java 微信支付随机字符串生成方法
- Java指定长度随机生成的字符串,且这个字符串必须包含大小写字母和数字
- Java生成随机字符串的代码
- Java生成定长随机字符串
- java uuid 随机生成唯一序列号
- Java中各种随机字符串生成的方法(亲测)
- Java生成随机字符串
- java生成指定长度的随机字符串
- java uuid 随机生成唯一序列号
- 一个生成java随机数字,数字字符串的方法
- Java生成随机字符串的代码
- java小例子:生成一个6位的随机字符串
- java随机生成字符串(字符随机生成类 生成随机字符组合)
- Java生成随机字符串,必须包含数字、小写字母、大写字母
- JAVA随机生成字符串
- java生成指定长度的随机字符串