本文實例為大家分享了Java實現搶紅包功能的具體代碼,供大家參考,具體內容如下
關鍵思想:
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()這個方法,由于是自動彈出隊首元素,必須做好同步機制,否則,當多個請求同時操作某一個紅包的最后一次剩余時,會造成總的紅包數量不正確。
(將加鎖的代碼注釋后,會發現打印的總數量有可能不正確了?。?/p>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。