Java類比搶紅包應用

來源:互聯網
上載者:User

近幾年來,春節搶紅包熱潮席捲全國。公司的遊戲也趕上了風潮,設計了搶紅包活動。

關鍵思想:

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<SpringGift> 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<SpringGift>();  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();  } }}
運行結果部分截圖如下

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

(將加鎖的代碼注釋後,會發現列印的總數量有可能不正確了。)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.