java多线程模拟抢红包功能
作者:淘金者05012 时间:2023-07-25 01:09:58
今天有朋友问我一道面试题,有5个人抢5个红包,可重复抢,用多线程程序实现,实现方式有多种,分享一下我的思路:应用了阻塞队列的特性。
/**
* Created by zhanglinqiang on 2016/6/23.
*/
public class MyTest {
public static void main(String[] args) throws InterruptedException {
LinkedBlockingQueue<LuckyMoney> luckyMoneys = new LinkedBlockingQueue<>();
List<FutureTask> futureTasks = new ArrayList<>();
//准备抢红包
for (int i = 0; i < 5; i++) {
FutureTask<Object> futureTask = new FutureTask<>(new CatchLuckMoney(luckyMoneys, "name" + i), null);
new Thread(futureTask,"name"+i).start();
futureTasks.add(futureTask);
}
Thread.sleep(5);//确保抢红包线程准备就绪
Random random = new Random(100);
//发5个红包
for (int i = 0; i < 5; i++) {
luckyMoneys.put(new LuckyMoney("红包"+i, random.nextInt(100)+1));
}
//等到红包抢完
while (!luckyMoneys.isEmpty()){
Thread.sleep(1);
}
//终止抢红包线程
for (FutureTask futureTask : futureTasks) {
futureTask.cancel(true);
}
}
}
class CatchLuckMoney implements Runnable {
public CatchLuckMoney(LinkedBlockingQueue<LuckyMoney> luckyMoneys, String name) {
this.luckyMoneys = luckyMoneys;
this.name = name;
}
private LinkedBlockingQueue<LuckyMoney> luckyMoneys;
private String name;
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
LuckyMoney redPackage = null;
try {
redPackage = luckyMoneys.take();
} catch (InterruptedException e) {
// e.printStackTrace();
break;
}
System.out.println(name + "抢到了-->" + redPackage);
luckyMoneys.remove(redPackage);
}
System.out.println("end>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+Thread.currentThread().getName());
}
}
class LuckyMoney {
public LuckyMoney(String name, Integer money) {
this.name = name;
this.money = money;
}
private String name;
private Integer money;
@Override
public String toString() {
return "LuckyMoney{" +
"name='" + name + '\'' +
", money=" + money +
'}';
}
}
运行效果:
Connected to the target VM, address: '127.0.0.1:8869', transport: 'socket'
name3抢到了-->LuckyMoney{name='红包0', money=16}
name4抢到了-->LuckyMoney{name='红包3', money=89}
name0抢到了-->LuckyMoney{name='红包2', money=75}
name1抢到了-->LuckyMoney{name='红包1', money=51}
name3抢到了-->LuckyMoney{name='红包4', money=92}
end>>>>>>>>>>>>>>>>>>>>>>>>>>>>name0
end>>>>>>>>>>>>>>>>>>>>>>>>>>>>name4
end>>>>>>>>>>>>>>>>>>>>>>>>>>>>name3
Disconnected from the target VM, address: '127.0.0.1:8869', transport: 'socket'
end>>>>>>>>>>>>>>>>>>>>>>>>>>>>name1
end>>>>>>>>>>>>>>>>>>>>>>>>>>>>name2
Process finished with exit code 0
来源:http://blog.csdn.net/u011411069/article/details/52503835
标签:java,抢红包
0
投稿
猜你喜欢
java volatile关键字的含义详细介绍
2021-11-01 16:52:17
Android基于ViewPager+Fragment实现左右滑屏效果的方法
2023-11-16 10:50:38
Java Runnable线程传参,实现让run访问参数
2023-09-04 21:36:42
Java SE求解汉诺塔问题的示例代码
2022-05-10 23:44:30
WPF使用WrapPanel实现虚拟化效果
2021-07-04 14:53:53
关注Ionic底部导航按钮tabs在android情况下浮在上面的处理
2023-10-01 05:38:17
如何用java实现分页查询
2023-09-23 06:50:24
springboot + mybatis + druid + 多数据源的问题详解
2023-01-24 15:59:58
java实现获取安卓设备里已安装的软件包
2022-10-21 00:53:04
c# 进程和线程的区别与联系
2023-05-04 23:18:36
Spring Boot 整合JPA 数据模型关联使用操作(一对一、一对多、多对多)
2022-11-18 23:14:29
使用maven创建web项目的方法步骤(图文)
2022-12-08 04:36:41
Android带进度条的文件上传示例(使用AsyncTask异步任务)
2023-06-24 09:43:11
C#解析json字符串总是多出双引号的原因分析及解决办法
2022-10-22 02:40:46
Android HorizontalScrollView滑动与ViewPager切换案例详解
2023-06-05 00:48:27
Maven入门之使用Nexus搭建Maven私服及上传下载jar包
2022-05-06 20:47:43
详解Spring Data JPA使用@Query注解(Using @Query)
2023-11-29 14:49:34
C#操作注册表的方法
2022-04-14 03:20:36
Android Fragment动态创建详解及示例代码
2023-04-26 00:25:33
Android中RecyclerView 滑动时图片加载的优化
2021-07-29 09:58:12