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

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

    设计如下:

    "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

 

 

    

转载于:https://www.cnblogs.com/yangzjcn/p/9064321.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值