Java生成訂單號/交易流水號

    分析:既然是訂單號/交易流水號,首先是不能重複,其次需考慮到性能問題。java

    設計以下:apache

    "HF"+時間戳+隨機數+循環數dom

    代碼以下:ide

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
相關文章
相關標籤/搜索