CountdownLatch 模拟 玩家就位

以下是代码
CountdownLatch是一个同步工具类,它可以使得等待几个线程一起执行完毕后,再开始执行后面的程序。


package com.test.thread01;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
// 程序开启入口
public class GameStart {

    public static void main(String args[]) {
        ExecutorService es = Executors.newFixedThreadPool(10);
        final CountDownLatch latch = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            Prole p = RoleFactory.createRole(i);
            RoleInitTask<Prole> task = new RoleInitTask<Prole>(latch, p);
            es.submit(task);
        }

        try {
            latch.await();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("所有玩家都到齐");

    }
}

英雄初始化

public class RoleInitTask<T extends Prole> implements Runnable {

    private final CountDownLatch latch;

    private T t;

    public RoleInitTask(CountDownLatch latch, T t) {
        this.latch = latch;
        this.t = t;
    }

    // 任务开始准备
    public void run() {
        t.init();// 初始化等待
        latch.countDown();
    }

}

角色

public abstract class Prole {

    private int hp;
    private String name;

    public Prole(int hp, String name) {
        this.hp = hp;
        this.name = name;
    }

    public abstract int decrOther(int rand);

    public abstract void init();

    public int getHp() {
        return hp;
    }

    public String getName() {
        return name;
    }

}
package com.test.thread01;

import java.util.Random;

public class Rrole extends Prole {

    public Rrole(int hp, String name) {
        super(hp, name);
    }

    @Override
    public int decrOther(int rand) {
        Random r = new Random();
        return r.nextInt(10);
    }

    @Override
    public void init() {
        Random r = new Random();
        int sleep = 200+ r.nextInt(2000);
        try {
            Thread.sleep(sleep);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println(this.getName() + " 初始化完成。");
    }

}

角色工厂

package com.test.thread01;

import java.util.Random;

public class RoleFactory {

    private RoleFactory() {

    }

    public static Prole createRole(int i) {
        Random r = new Random();
        int hp = 100 + r.nextInt(20);
        return new Rrole(hp, "name" + i);
    }
}

git地址

https://github.com/txcname/fly

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CountDownLatch 是 Java 中的一个并发工具类,用于实现线程间的协调和控制。它的主要作用是让某个线程等待其他线程执行完毕之后再继续执行。 下面是一个使用 CountDownLatch 进行并发模拟的例子: ```java import java.util.concurrent.CountDownLatch; public class ConcurrencySimulation { public static void main(String[] args) throws InterruptedException { int threadCount = 5; // 线程数量 // 创建一个 CountDownLatch,初始计数为线程数量 CountDownLatch latch = new CountDownLatch(threadCount); for (int i = 0; i < threadCount; i++) { Thread thread = new Thread(new Worker(latch)); thread.start(); } // 主线程等待所有子线程执行完毕 latch.await(); System.out.println("所有线程执行完毕"); } static class Worker implements Runnable { private final CountDownLatch latch; public Worker(CountDownLatch latch) { this.latch = latch; } @Override public void run() { try { // 模拟线程执行耗时操作 Thread.sleep(1000); System.out.println("线程执行完毕"); } catch (InterruptedException e) { e.printStackTrace(); } finally { // 每个线程执行完毕后将计数器减一 latch.countDown(); } } } } ``` 这个例子创建了 5 个子线程,每个子线程执行耗时操作后将计数器减一。主线程调用 `latch.await()` 方法,等待所有子线程执行完毕后输出 "所有线程执行完毕"。通过使用 CountDownLatch,可以实现简单的并发模拟线程间的协调控制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值