0

我有两个每秒运行的执行器服务。但是当我在 run 方法中插入一行代码时,其中一个停止运行。这是我的课程:

游戏服务器:

public class GameServer implements Runnable {

    private ServerManager server;
    private int id;

    public GameServer(ServerManager sm) {
        server = sm;
        id = server.idCreator();
        server.registerGameServer(this);
    }

    @Override
    public void run() {
        System.out.println("Game server is collecting data every second");
    }

    public int getId() {
        return id;
    }
}

服务器管理器:

import java.util.HashMap;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

public class ServerManager {

    private final AtomicInteger count = new AtomicInteger(0);
    private HashMap<Integer, GameServer> listofGameServers = new HashMap<>();

    public ServerManager() {
    }

    public void registerGameServer(GameServer gs) {
        listofGameServers.put(gs.getId(), gs);
    }

    public HashMap<Integer, GameServer> getGameServers() {
        return new HashMap<>(listofGameServers);
    }

    public int idCreator() {
        synchronized (count) {
            return count.incrementAndGet();
        }
    }

    public int generateRandomNumber(int start, int end) {
        Random rand = new Random();
        int number = start + rand.nextInt(end - start + 1);
        return number;
    }
}

玩家:

import java.util.HashMap;

public class Player implements Runnable {

    private int id;
    private ServerManager server;

    public Player(ServerManager sm) {
        this.server = sm;
        this.id = server.idCreator();
    }

    @Override
    public void run() {
        System.out.println("Player running every second");

        HashMap<Integer, GameServer> listOfGameServers = server.getGameServers();

        //int randomGameServer = listOfGameServers.get(server.generateRandomNumber(0, listOfGameServers.size()-1)).getId();
        int a = server.generateRandomNumber(0, listOfGameServers.size() - 1);
        System.out.println("Player selected a server: " + a);
    }

    public int getId() {
        return id;
    }
}

主要的:

import java.util.concurrent.*;

public class TestDriver {

    public static void main(String[] args) {
        ServerManager server = new ServerManager();

        ScheduledExecutorService game = Executors.newScheduledThreadPool(3);
        for (int j = 0; j < 3; j++) {
            game.scheduleAtFixedRate(new GameServer(server), 0, 1, TimeUnit.SECONDS);
        }

        ScheduledExecutorService prodExec = Executors.newScheduledThreadPool(3);
        prodExec.scheduleAtFixedRate(new Player(server), 0, 1, TimeUnit.SECONDS);
    }
}

Player类中,有一行注释的代码int randomGameServer =...run 方法按预期每秒流畅运行。但是如果我取消注释,播放器运行的方法只会执行几次并停止。我想做的是让玩家随机选择一个游戏服务器加入。有谁知道它为什么停止?

带有注释行的结果(播放器连续运行):

Game server is collecting data every second
Game server is collecting data every second
Game server is collecting data every second
Player running every second
Player selected a server: 0
Game server is collecting data every second
Game server is collecting data every second
Game server is collecting data every second
Player running every second
Player selected a server: 2
Game server is collecting data every second
Game server is collecting data every second
Game server is collecting data every second
Player running every second
Player selected a server: 2
Game server is collecting data every second
Game server is collecting data every second
Game server is collecting data every second
Player running every second
Player selected a server: 1
Game server is collecting data every second
Game server is collecting data every second
Game server is collecting data every second
Player running every second
Player selected a server: 0
Game server is collecting data every second
Game server is collecting data every second
Game server is collecting data every second
Player running every second
Player selected a server: 0

未注释行的结果(播放器运行了几次):

Game server is collecting data every second
Game server is collecting data every second
Game server is collecting data every second
Player running every second
Player selected a server: 1
Game server is collecting data every second
Game server is collecting data every second
Game server is collecting data every second
Player running every second
Game server is collecting data every second
Game server is collecting data every second
Game server is collecting data every second
Game server is collecting data every second
Game server is collecting data every second
Game server is collecting data every second
Game server is collecting data every second
4

1 回答 1

1

你从来没有打电话registerGameServer(),所以HashMap它仍然是空的,做get()会给你一个空结果。称getId()原因为NullPointerException.

一旦你解决了它,看看你的程序的效率。无需HashMap每次都创建一个新的,获取相同的并通过使用并发版本使其线程安全。

此外,对于Random,而不是每次都创建一个新的,使用ThreadLocalRandom.

于 2012-11-11T04:08:56.083 回答