服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - JAVA教程 - java多线程模拟抢红包功能

java多线程模拟抢红包功能

2021-03-04 09:48淘金者05012 JAVA教程

这篇文章主要为大家详细介绍了java多线程模拟抢红包功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

今天有朋友问我一道面试题,有5个人抢5个红包,可重复抢,用多线程程序实现,实现方式有多种,分享一下我的思路:应用了阻塞队列的特性。

java" id="highlighter_808290">
?
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
 * 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 +
    '}';
 }
}

运行效果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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

延伸 · 阅读

精彩推荐