采用多线程模拟多人同时抢红包。服务端将玩家发出的红包保存在一个队列里,然后用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<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()这个方法,由于是自动弹出队首元素,必须做好同步机制,否则,当多个请求同时操作某一个红包的最后一次剩余时,会造成总的红包数量不正确。

(将加锁的代码注释后,会发现打印的总数量有可能不正确了!)

最新文章

  1. windows下配置apache+php环境
  2. C# 常用加密解密帮助类
  3. Wb应用程序开放原理
  4. XML 链接
  5. iOS图像资源Images Assets
  6. 深度理解依赖注入(Dependence Injection)
  7. Cookie和Session简介与区别
  8. NYOJ 38 布线问题_(解法2 Prim算法)
  9. Delphi之TreeView
  10. VIM用法
  11. .NET下发送邮件遇到问题及解决方案
  12. CentOS 7 系统下 GitLab 搭建
  13. 用JDOM和DOM4J解析节点名节点值
  14. python服务器文件上传下载+GUI【tkinter】
  15. BZOJ-3-1010: [HNOI2008]玩具装箱toy-斜率优化DP
  16. Bellman-Ford算法模板题
  17. ESET NOD32 Antivirus – 免费 3个月/ 3PC
  18. springMVC学习(5)-参数绑定
  19. 【转】.net 实现 语音搜索(仅限WebKit内核浏览器)
  20. 1555: Inversion Sequence (通过逆序数复原序列 vector的骚操作!!!)

热门文章

  1. cluster模块设置子进程的stdio
  2. 2019 ICPC上海网络赛 A 题 Lightning Routing I (动态维护树的直径)
  3. LightOJ1199 Partition Game
  4. 通过Centreon监控apache、MySQL、Hadoop服务状态
  5. 基于icamera usb2.0的视频采集系统之mt9m001c12stc测评
  6. JavaScript 逻辑与(&amp;&amp;) 与 逻辑或(||) 运算规则
  7. Jquery使用ajax与Flask后端进行数据交互
  8. 实习生4面美团Java岗,已拿offer!(框架+多线程+集合+JVM)
  9. NodeJS4-8静态资源服务器实战_构建cli工具
  10. NodeJS4-1静态资源服务器实战_实现访问获取里面的内容