当前位置: 移动技术网 > IT编程>开发语言>Java > Java生成订单号/交易流水号

Java生成订单号/交易流水号

2018年05月21日  | 移动技术网IT编程  | 我要评论

朝天锅是哪个地方的菜,蛇王站好趴下,薛蒙

    分析:既然是订单号/交易流水号,首先是不能重复,其次需考虑到性能问题。

    设计如下:

    "HF"+时间戳+随机数+循环数

    代码如下:

1 int x = 1000;
2 for(int i=0;i<10;i++){
3     x+=1;   
4     System.out.println("HF"+System.currentTimeMillis()+RandomUtils.getNo(2)+x);
5 }

    其中:RandomUtils类

 1 package com.test.common.util;
 2 
 3 import org.apache.commons.lang.RandomStringUtils;
 4 
 5 public class RandomUtils
 6 {
 7     private static String randString = "";
 8 
 9     public synchronized static String getNo(int k)
10     {
11         if (randString.length() > 20000)
12         {
13             randString = "";
14         }
15         String rno = getNoNo(k);
16         while (randString.indexOf(rno + ",") >= 0)
17         {
18             rno = getNoNo(k);
19         }
20         randString += rno + ",";
21         return rno;
22     }
23 
24     private static String getNoNo(int k)
25     {
26         try
27         {
28             Thread.sleep(1);
29         }
30         catch (InterruptedException e)
31         {
32             e.printStackTrace();
33         }
34         return RandomStringUtils.randomNumeric(k);
35     }
36 }
RandomUtils.java

 

 

    

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网