gpt4 book ai didi

java - ScheduledExecutorService 一段时间后未运行

转载 作者:行者123 更新时间:2023-12-01 15:03:16 26 4
gpt4 key购买 nike

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

游戏服务器:

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

最佳答案

您永远不会调用 registerGameServer(),因此 HashMap 保持为空,并且执行 get() 会给您一个空结果。调用 getId() 会导致 NullPointerException

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

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

关于java - ScheduledExecutorService 一段时间后未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13328386/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com