zoukankan      html  css  js  c++  java
  • java多线程开发之通过对战游戏学习CyclicBarrier

    java多线程开发之通过对战游戏学习CyclicBarrier:

    构造方法:
    CyclicBarrier(int parties):
    创建一个新的 CyclicBarrier,当给定数量的线程(线程)等待他时候,他将跳闸
    并且当屏障跳闸时候不执行预定的动作.
    CyclicBarrier(int parties,Runnable barrierAction):
    创建一个新的 CyclicBarrier ,当给定数量的线程(线程)等待时,它将跳闸,当屏障跳闸时执行给定的屏障动作,由最后一个进入屏障的线程执行。

    接口方法:

    需求:

    继上一篇CountDownLatch模拟游戏加载后,现在用户点击开始按钮后,需要匹配包括自己在内的五个玩家才能开始游戏,匹配玩家成功后进入到选择角色阶段。当5位玩家角色都选择完毕后,开始进入游戏。进入游戏时需要加载相关的数据,待全部玩家都加载完毕后正式开始游戏。

    解决方案

    从需求中可以知道,想要开始游戏需要经过三个阶段,分别是

    匹配玩家

    选择角色

    加载数据

    在这三个阶段中,都需要互相等待对方完成才能继续进入下个阶段。

    这时可以采用CyclicBarrier来作为各个阶段的节点,等待其他玩家到达,在进入下个阶段。

     代码演示:

      1 package com.heima.duotai1;
      2 import java.io.IOException;
      3 import java.util.concurrent.CyclicBarrier;
      4 import java.util.concurrent.locks.Lock;
      5 import java.util.concurrent.locks.ReentrantLock;
      6 
      7 public class StartGame implements Runnable {
      8     int ticket=0;
      9     Object obj=new Object();
     10     private String player;//定义玩家
     11     private CyclicBarrier barrier;//barrier障碍物
     12 
     13     //空参数构造方法
     14     public StartGame() {
     15     }
     16 
     17     //满参数构造方法
     18     public StartGame(String player, CyclicBarrier barrier) {
     19         this.player = player;
     20         this.barrier = barrier;
     21     }
     22 
     23     public String getPlayer() {
     24         return player;
     25     }
     26 
     27     public void setPlayer(String player) {
     28         this.player = player;
     29     }
     30 
     31     public CyclicBarrier getBarrier() {
     32         return barrier;
     33     }
     34 
     35     public void setBarrier(CyclicBarrier barrier) {
     36         this.barrier = barrier;
     37     }
     38 
     39     @Override
     40     public void run() {
     41         //设置窗口
     42 
     43         while (true)
     44         if (ticket>=0) {
     45                 try {
     46 
     47                     System.out.println(this.getPlayer() + "开始匹配玩家。。。");
     48                     barrier.await();
     49                     Thread.sleep(100);
     50                     findOtherPlayer();
     51                     System.out.println(this.getPlayer() + "进行选择游戏角色。。。");
     52                     barrier.await();
     53                     Thread.sleep(100);
     54                     choiceRole();
     55                     System.out.println(this.getPlayer() + "开始进行游戏加载。。。");
     56                     Thread.sleep(100);
     57                     loading();
     58                     System.out.println(this.getPlayer() + "游戏加载完毕等待其他玩家加载完成。。。");
     59                     barrier.await();
     60                     StartGame b = new StartGame();
     61                     Thread a = new Thread(b);
     62                     a.start();
     63                 } catch (Exception e) {
     64                     e.printStackTrace();
     65                 }
     66 
     67         }
     68     }
     69     private void loading() {
     70 
     71     }
     72 
     73     private void choiceRole() {
     74 
     75     }
     76 
     77     private void findOtherPlayer() {
     78 
     79     }
     80 
     81     public static void main(String[] args) throws IOException {
     82         CyclicBarrier barrier=new CyclicBarrier(5,()->{
     83             try {
     84                 System.out.println("阶段完成,等待2秒");
     85                 Thread.sleep(2000);
     86                 System.out.println("进入下个阶段");
     87             } catch (InterruptedException e) {
     88                 e.printStackTrace();
     89             }
     90         });
     91         Thread thread = new Thread(new StartGame("张无忌", barrier));
     92         Thread thread1 = new Thread(new StartGame("张三丰", barrier));
     93         Thread thread2 = new Thread(new StartGame("皱滋润", barrier));
     94         Thread thread3 = new Thread(new StartGame("周子若", barrier));
     95         Thread thread4 = new Thread(new StartGame("赵敏", barrier));
     96         thread.start();
     97         thread1.start();
     98         thread2.start();
     99         thread3.start();
    100         thread4.start();
    101         System.in.read();
    102     }
    103 }
  • 相关阅读:
    preprocess
    数组
    共用体
    动态内存管理函数
    C链表
    文件的定位与出错检查
    字符串读写函数
    C文件操作
    位运算
    爱好-超级IP:超级IP
  • 原文地址:https://www.cnblogs.com/luliang1215/p/10636441.html
Copyright © 2011-2022 走看看