多线程-赛马游戏

 
  1. package easy;

  2.  
  3. import java.util.concurrent.ArrayBlockingQueue;

  4. import java.util.concurrent.ThreadPoolExecutor;

  5. import java.util.concurrent.TimeUnit;

  6.  
  7. public class Solution66 {

  8. //初始化线程数

  9. public static final int NUMBER = 3;

  10.  
  11. public static void main(String[] args) {

  12. //初始化线程池

  13. ThreadPoolExecutor executor = new ThreadPoolExecutor(NUMBER, NUMBER, 300,

  14. TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(3),

  15. new ThreadPoolExecutor.CallerRunsPolicy());

  16. System.out.println("---技能描述:@千里奔袭(突刺一段随机距离)");

  17. System.out.println("---马匹准备就绪,比赛开始");

  18. //从池中生成三个线程

  19. for (int i = 0; i < NUMBER; i++) {

  20. executor.execute(new StartGame());

  21. }

  22. executor.shutdown();

  23. }

  24.  
  25. }

  26.  

 

 
  1. package easy;

  2.  
  3. public class StartGame extends Thread {

  4. //初始化终点和两匹马的当前路程

  5. static int destination = 30;

  6. static int horseDes1 = 0;

  7. static int horseDes2 = 0;

  8. //记录技能的使用情况

  9. static boolean horse1UseSkill = true;

  10. static boolean horse2UseSkill = true;

  11. String horse1UseSkillMsg = "";

  12. String horse2UseSkillMsg = "";

  13. //记录弹射起步的状态

  14. static boolean status = true;

  15. //记录播报比赛结果的状态

  16. static boolean matchStatus = true;

  17. public static final Object WAIT_MONITOR = new Object();

  18.  
  19. @Override

  20. public void run() {

  21. StartGame game = new StartGame();

  22.  
  23. synchronized(WAIT_MONITOR) {

  24. if (status) {

  25. game.jump();

  26. }

  27. }

  28. try{

  29. game.listenHalf();

  30. game.match();

  31. Thread.sleep(500);

  32. synchronized(WAIT_MONITOR) {

  33. if (matchStatus) {

  34. game.showResult();

  35. }

  36. }

  37. }catch (Exception e){

  38. }

  39. }

  40. //弹射起步福利

  41. public void jump(){

  42. if ((Thread.currentThread().getName() + "马").equals("pool-1-thread-1马")) {

  43. System.out.println(Thread.currentThread().getName() + "马率先弹射起步,前进了5m");

  44. horseDes1 = horseDes1 + (int)(Math.random()*10);

  45. status = false;

  46. }else if ((Thread.currentThread().getName() + "马").equals("pool-1-thread-2马")) {

  47. System.out.println(Thread.currentThread().getName() + "马率先弹射起步,前进了5m");

  48. horseDes2 = horseDes2 + (int)(Math.random()*10);

  49. status = false;

  50. }

  51. }

  52.  
  53. //每隔5s,播报比赛状态

  54. public void match() throws InterruptedException {

  55. while (horseDes1 < destination && horseDes2 < destination) {

  56. Thread.sleep(5000);

  57. if ((Thread.currentThread().getName() + "马").equals("pool-1-thread-1马")) {

  58. horseDes1 = horseDes1 + (int)(Math.random()*10);

  59. //技能使用情况

  60. if ((int)(Math.random()*10) % 2 == 0 && horse1UseSkill) {

  61. horseDes1 = horseDes1 + (int)(Math.random()*10);

  62. horse1UseSkillMsg = ",并在此次行动使用了@千里奔袭技能";

  63. horse1UseSkill = false;

  64. }

  65. System.out.println(Thread.currentThread().getName() + "马目前前进了" + horseDes1 +"米" + horse1UseSkillMsg);

  66. horse1UseSkillMsg = "";

  67. }else if ((Thread.currentThread().getName() + "马").equals("pool-1-thread-2马")) {

  68. horseDes2 = horseDes2 + (int)(Math.random()*10);

  69. //技能使用情况

  70. if ((int)(Math.random()*10) % 2 == 0 && horse2UseSkill) {

  71. horseDes2 = horseDes2 + (int)(Math.random()*10);

  72. horse2UseSkillMsg = ",并在此次行动使用了@千里奔袭技能";

  73. horse2UseSkill = false;

  74. }

  75. System.out.println(Thread.currentThread().getName() + "马目前前进了" + horseDes2 +"米" + horse2UseSkillMsg);

  76. horse2UseSkillMsg = "";

  77. }

  78. }

  79. }

  80.  
  81. //播报比赛结果

  82. public void showResult() {

  83. if (horseDes1 >= destination && (Thread.currentThread().getName() + "马").equals("pool-1-thread-1马")) {

  84. System.out.println(Thread.currentThread().getName() + "马赢得了比赛,恭喜它!");

  85. matchStatus = false;

  86. }else if (horseDes2 >= destination && (Thread.currentThread().getName() + "马").equals("pool-1-thread-2马")) {

  87. System.out.println(Thread.currentThread().getName() + "赢得了比赛,恭喜它!");

  88. matchStatus = false;

  89. }

  90. }

  91.  
  92. //新起一个线程,用来监听赛程过半消息

  93. public void listenHalf() {

  94. if ((Thread.currentThread().getName() + "马").equals("pool-1-thread-3马")) {

  95. while (true) {

  96. if (horseDes1 >= destination / 2) {

  97. System.out.println("---赛程已过半,比赛进入白热化阶段");

  98. break;

  99. }else if (horseDes2 >= destination / 2) {

  100. System.out.println("---赛程已过半,比赛进入白热化阶段");

  101. break;

  102. }

  103. }

  104. }

  105. }

  106. }

效果图如下所示:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值