1. 程式人生 > >java 產生紅包多執行緒

java 產生紅包多執行緒

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;

public class RedEnvelopesFactory {
    // 紅包
    private RedEnvelopes redEnvelopes;
    // 個數
    private Integer num;

    private RedEnvelopesFactory(RedEnvelopes redEnvelopes, Integer num) {
        this.redEnvelopes = redEnvelopes;
        this.num = num;
    }


    static ConcurrentLinkedQueue<RedEnvelopes> redEnvelopesConcurrentLinkedQueue = new ConcurrentLinkedQueue<>();

    public static RedEnvelopes getRedEnvelopes() {

        RedEnvelopes redEnvelopes = redEnvelopesConcurrentLinkedQueue.poll();
        if (redEnvelopes == null) {
            addRedEnvelopesConcurrentLinkedQueue();
            redEnvelopes = getRedEnvelopes();
        }
        System.out.println("使用者 執行緒 :"+Thread.currentThread().getName()+"  獲得紅包  "+redEnvelopes.toString());
        return redEnvelopes;
    }

   static int i=0;//測試用
    public static void addRedEnvelopesConcurrentLinkedQueue() {
        synchronized (redEnvelopesConcurrentLinkedQueue) {
            if (redEnvelopesConcurrentLinkedQueue.isEmpty()) {
                i=i+1;
                System.out.println("進入到紅包佇列構造工廠的次數"+i);
                redEnvelopesConcurrentLinkedQueue.addAll(BuildRedEnvelopesList.getRedEnvelopesSet(1));
                System.out.println("紅包佇列構造工廠的構造紅包"+redEnvelopesConcurrentLinkedQueue.size()+"個");
            }
        }
    }


    private static class BuildRedEnvelopesList {
        final static Map<Integer, RedEnvelopesFactory> redEnvelopesTypeMap = new HashMap<>();
        final static List<RedEnvelopes> redEnvelopesList = new LinkedList<>();

        static {
            // 應該讀取你的配置去構造這個map
            redEnvelopesTypeMap.put(1, new RedEnvelopesFactory(new RedEnvelopes(1, 3.8), 5));

            redEnvelopesTypeMap.put(2, new RedEnvelopesFactory(new RedEnvelopes(2, 6.6), 10));

            redEnvelopesTypeMap.put(3, new RedEnvelopesFactory(new RedEnvelopes(3, 8.8), 10));

            redEnvelopesTypeMap.put(4, new RedEnvelopesFactory(new RedEnvelopes(4, 10.0), 9));

            redEnvelopesTypeMap.put(5, new RedEnvelopesFactory(new RedEnvelopes(5, 16.0), 5));

            redEnvelopesTypeMap.put(6, new RedEnvelopesFactory(new RedEnvelopes(6, 66.0), 1));

            redEnvelopesTypeMap.keySet().forEach(x -> {
                RedEnvelopesFactory redEnvelopesFactory = redEnvelopesTypeMap.get(x);
                for (int i = 0; i < redEnvelopesFactory.num; i++) {
                    redEnvelopesList.add(redEnvelopesFactory.redEnvelopes);
                }
            });
        }

        /**
         * 獲得紅包池
         *
         * @param num 紅包池個數
         * @return
         */
        static List<RedEnvelopes> getRedEnvelopesSet(Integer num) {
            num = Optional.ofNullable(num).orElse(1);
            List<RedEnvelopes> rList = new LinkedList<>();
            for (int i = 0; i < num; i++) {
                rList.addAll(redEnvelopesList);
            }
            return rList;
        }
    }


    public static void main(String[] args) {
        for (int j = 0; j < 400; j++) {
            new Thread(()->{
                getRedEnvelopes();
            }).start();
        }
    }


}

 

 

public class RedEnvelopes {

    private Integer moneyType;
    private Double money;

    @Override
    public String toString() {
        return "RedEnvelopes{" +
                "moneyType=" + moneyType +
                ", money=" + money +
                '}';
    }

    public Integer getMoneyType() {
        return moneyType;
    }

    public void setMoneyType(Integer moneyType) {
        this.moneyType = moneyType;
    }

    public Double getMoney() {
        return money;
    }


    public void setMoney(Double money) {
        this.money = money;
    }


    public RedEnvelopes(){

    }
    public RedEnvelopes(Integer moneyType, Double money) {
        this.moneyType = moneyType;
        this.money = money;
    }

}