1. 程式人生 > 其它 >java實現qq搶紅包_Java實現搶紅包功能--趣味知識分享

java實現qq搶紅包_Java實現搶紅包功能--趣味知識分享

技術標籤:java實現qq搶紅包

採用多執行緒模擬多人同時搶紅包。服務端將玩家發出的紅包儲存在一個佇列裡,然後用Job定時將紅包資訊推送給玩家。每一批玩家的搶紅包請求,其實操作的都是從佇列中彈出的第一個紅包元素,但當前的紅包數量為空的時候,自動彈出下一個紅包(如果有的話)。

關鍵思想:

1.搶紅包涉及多人併發操作,需要做好同步保證多執行緒執行結果正確。

2.由於同時線上人數大,從效能方面考慮,玩家的發紅包請求不必及時響應,而由服務端定時執行發紅包佇列。

下面是主要的程式碼和實現邏輯說明

1.建立一個類,表示紅包這個實體概念。直接採用原子變數保證增減同步。Java的原子變數是一種精度更細的同步機制,在高度競爭的情況下,鎖的效能將超過原子變數的效能,但在更真實的競爭情況,原子變數享有更好的效能。

public class SpringGift { private String role; private AtomicInteger gift; public String getRole() { return role; } public void setRole(String role) { this.role = role; } public AtomicInteger getGift() { return gift; } public void setGift(AtomicInteger gift) { this.gift = gift; } public int getRemainCount(){ return this.gift.get(); }}

2.採用多執行緒模擬多人同時搶紅包。服務端將玩家發出的紅包儲存在一個佇列裡,然後用Job定時將紅包資訊推送給玩家。每一批玩家的搶紅包請求,其實操作的都是從佇列中彈出的第一個紅包元素,但當前的紅包數量為空的時候,自動彈出下一個紅包(如果有的話)。

public class Test { public static ConcurrentLinkedQueue queue; public static SpringGift currGift; public static AtomicInteger count = new AtomicInteger(); static class myThread implements Runnable{ public void run(){ handleEvent(); } } public static void main(String[] args) throws Exception { queue = new ConcurrentLinkedQueue(); for(int i =0;i<3;i++){ SpringGift gift = new SpringGift(); gift.setRole("role"+i); gift.setGift(new AtomicInteger(50)); queue.add(gift); } myThread mythread = new myThread(); for(int i=0;i<1000;i++){ new Thread(mythread).start(); }  System.err.println("總共收到"+count.get()); } private static SpringGift getGift(){ //防止多條執行緒同時彈出隊首 synchronized (queue) {//若沒有加鎖,列印的count總數不對!!!! if(currGift == null || currGift.getRemainCount() <=0){ currGift = queue.poll(); } } return currGift; } public static void handleEvent(){ try{ SpringGift obj = getGift();  if(obj == null || obj.getRemainCount() <= 0){ System.err.println("沒有了"); return ; } if(obj !=null && obj.getGift().getAndDecrement() >0 ){ System.err.println("搶到一個紅包"); count.getAndIncrement(); } Thread.sleep(500);//模擬處理其他操作 }catch(Exception e){ e.printStackTrace(); } }}

執行結果部分截圖如下

7c7e89ebeec4702378f614765f8ff724.png

需要注意的是,getGift()這個方法,由於是自動彈出隊首元素,必須做好同步機制,否則,當多個請求同時操作某一個紅包的最後一次剩餘時,會造成總的紅包數量不正確。

(將加鎖的程式碼註釋後,會發現列印的總數量有可能不正確了!)

路在腳下

鳴謝作者:探歌

原文:https://www.cnblogs.com/lgg20/p/12010628.html