本文實(shí)例為大家分享了java實(shí)現(xiàn)搶紅包功能的具體代碼,供大家參考,具體內(nèi)容如下
關(guān)鍵思想:
1.搶紅包涉及多人并發(fā)操作,需要做好同步保證多線(xiàn)程運(yùn)行結(jié)果正確。
2.由于同時(shí)在線(xiàn)人數(shù)大,從性能方面考慮,玩家的發(fā)紅包請(qǐng)求不必及時(shí)響應(yīng),而由服務(wù)端定時(shí)執(zhí)行發(fā)紅包隊(duì)列。
下面是主要的代碼和實(shí)現(xiàn)邏輯說(shuō)明
1.創(chuàng)建一個(gè)類(lèi),表示紅包這個(gè)實(shí)體概念。直接采用原子變量保證增減同步。java的原子變量是一種精度更細(xì)的同步機(jī)制,在高度競(jìng)爭(zhēng)的情況下,鎖的性能將超過(guò)原子變量的性能,但在更真實(shí)的競(jìng)爭(zhēng)情況,原子變量享有更好的性能。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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.采用多線(xiàn)程模擬多人同時(shí)搶紅包。服務(wù)端將玩家發(fā)出的紅包保存在一個(gè)隊(duì)列里,然后用job定時(shí)將紅包信息推送給玩家。每一批玩家的搶紅包請(qǐng)求,其實(shí)操作的都是從隊(duì)列中彈出的第一個(gè)紅包元素,但當(dāng)前的紅包數(shù)量為空的時(shí)候,自動(dòng)彈出下一個(gè)紅包(如果有的話(huà))。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
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(){ //防止多條線(xiàn)程同時(shí)彈出隊(duì)首 synchronized (queue) { //若沒(méi)有加鎖,打印的count總數(shù)不對(duì)!?。?! 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( "沒(méi)有了" ); return ; } if (obj != null && obj.getgift().getanddecrement() > 0 ){ system.err.println( "搶到一個(gè)紅包" ); count.getandincrement(); } thread.sleep( 500 ); //模擬處理其他操作 } catch (exception e){ e.printstacktrace(); } } } |
運(yùn)行結(jié)果部分截圖如下
需要注意的是,getgift()這個(gè)方法,由于是自動(dòng)彈出隊(duì)首元素,必須做好同步機(jī)制,否則,當(dāng)多個(gè)請(qǐng)求同時(shí)操作某一個(gè)紅包的最后一次剩余時(shí),會(huì)造成總的紅包數(shù)量不正確。
(將加鎖的代碼注釋后,會(huì)發(fā)現(xiàn)打印的總數(shù)量有可能不正確了!)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/littleschemer/article/details/46382117