随机抽奖号码池子工具类(简单版)

RandomLotteryNumberUtil.java

import java.util.ArrayList;

import java.util.Collections;

import java.util.HashSet;

import java.util.List;

import java.util.Set;

 

/**

 * 获取随机中奖码(100001~999999

 *

 * @ClassName: RandomLotteryNumberUtil

 * @Description: 随机中奖码获取工具。先生成70万的中奖号码池,每次从池子里面获取

 * @author: yujie020 yujie020@sina.com

 * @date: 2016 -3 -8 上午10:00:40

 */

public class RandomLotteryNumberUtil {

 

     private static volatile Set<Integer> LOTTERY_NUMBER_SET;

 

     static {

            LOTTERY_NUMBER_SET = new HashSet<Integer>();

            do {

                 int num = ( int) (Math. random() * (999999 - 100001)) + 100001; // 最大减去最小

                 LOTTERY_NUMBER_SET.add(num); // 保证将不同的数存入HashSet

           } while ( LOTTERY_NUMBER_SET.size() < 700000);

     }

 

     /**

      * 从池子里面获取指定数目的抽奖号码

      *

      * @Title: getLotteryNumber

      * @Description: 从池子里面获取指定数目的抽奖号码

      * @param getCount

      *            获取的条数

      * @return: List<Integer>

      */

     public static synchronized List<Integer> getLotteryNumber(int getCount) {

            // 如果当前随机码池子数为0,则返回空

            if ( LOTTERY_NUMBER_SET.size() == 0) {

                 return null;

           }

 

            // 获取列表形式的池子

           ArrayList<Integer> lotteryNumberList = new ArrayList<>(

                      LOTTERY_NUMBER_SET);

           Collections. shuffle(lotteryNumberList);// 打乱list顺序

 

            // 如果获取的抽奖码数目大于剩余数目,则清除池子所有值,返回List

            if (getCount > LOTTERY_NUMBER_SET.size()) {

                 LOTTERY_NUMBER_SET.clear();

                 return lotteryNumberList;

           } else { // 如果获取的抽奖码数目大于剩余数目小于等于池子剩余的数目则截取出指定数目的列表数据

                List<Integer> tempList = lotteryNumberList.subList(0, getCount);

                 for (Integer tmpInteger : tempList) {

                      removeLotteryNumber(tmpInteger);

                }

 

                 return tempList;

           }

     }

 

     /**

      *

      * @Title: removeLotteryNumber

      * @Description: TODO

      * @param num

      * @return

      * @return: boolean

      */

     private static boolean removeLotteryNumber(Integer num) {

            synchronized (RandomLotteryNumberUtil. class) {

                 return LOTTERY_NUMBER_SET.remove(num);

           }

 

     }

 

     public static void main(String[] args) throws InterruptedException {

            // List lotteryNumberList = new

            // ArrayList<>(RandomLotteryNumberUtil.LOTTERY_NUMBER_SET);

           System. out.println( LOTTERY_NUMBER_SET.size());

           ThreadTest th1 = new ThreadTest();

           ThreadTest th2 = new ThreadTest();

           ThreadTest th3 = new ThreadTest();

 

           th1.start();

           th2.start();

           th3.start();

 

           Thread. sleep(1000);

          System. out.println(RandomLotteryNumberUtil.LOTTERY_NUMBER_SET.size());

 

     }

 

}

 

 

测试类:

ThreadTest.java

public class ThreadTest extends Thread{

 

     @Override

     public void run() {

            synchronized (ThreadTest. class) {

                

                System. out.println( this.currentThread().getName() +":"+RandomLotteryNumberUtil.getLotteryNumber(30));

                

           }

           

     }

}

 

展开阅读全文

没有更多推荐了,返回首页