Java BlockingQueue消息线程之间的延迟问题

Java BlockingQueue消息线程之间的延迟问题,java,multithreading,sockets,threadpool,blockingqueue,Java,Multithreading,Sockets,Threadpool,Blockingqueue,我用套接字线程池实现了一个两人游戏。每个玩家都连接到自己的线程。我根据文章添加了一个消息队列系统 问题是信息滞后。来自第一个播放器的第一个响应按预期添加到messageQueue。但第二个播放器并没有通过调用poll()接收到它,它只接收空值。 然后第二个玩家响应,第二个玩家接收第一个玩家消息。我的目的是在第二个玩家做出回应之前将信息发送给他/她 我一定是搞错了,不然我就忽略了一些重要的概念 你能帮我找到它吗 我的代码是这样的,有两个类与之相关,GameRunnable.java和Game.ja

我用套接字线程池实现了一个两人游戏。每个玩家都连接到自己的线程。我根据文章添加了一个消息队列系统

问题是信息滞后。来自第一个播放器的第一个响应按预期添加到messageQueue。但第二个播放器并没有通过调用
poll()
接收到它,它只接收空值。 然后第二个玩家响应,第二个玩家接收第一个玩家消息。我的目的是在第二个玩家做出回应之前将信息发送给他/她

我一定是搞错了,不然我就忽略了一些重要的概念

你能帮我找到它吗

我的代码是这样的,有两个类与之相关,GameRunnable.java和Game.java。我省略了一些代码来简化这一混乱局面

在游戏可运行类中

public static final Map<GamerRunnable, BlockingQueue<String>> messageQueues = new ConcurrentHashMap<>();
public static final Map<String, GamerRunnable> gameQueue = new ConcurrentHashMap<>();
private Game game;


public void run() {
    System.out.println
        (this.setGameInstance(clientSocket, readerIn, output) ? "OK": "FAILED");
    messageQueues.put(this, new ArrayBlockingQueue<String>(100));

    // If player 1
    this.game.initGame(this);

    // If Player 2
    this.game.initGame(this);
}

public static GamerRunnable getGameThreadByName(String name) {
    return gameQueue.get(name);
}

public String getName() {
    return this.name;
}

由于网络延迟,您可能需要短暂的延迟来等待第一条消息到达队列。试试看。这将等待指定的时间,以便消息出现在队列上。如果没有任何内容,它将返回null,就像poll()现在一样。如果没有可用消息,用户可能不会注意到500毫秒到1秒的延迟

public Game(Socket clientSocket, BufferedReader readIn, OutputStream output) {
    this.sockGamer = clientSocket;

    try {
        this.out = output;
        this.inGamer = readIn; 
    } catch(Exception e) {
        e.printStackTrace();
    }

    public void sendToGamer(String msg) {
        try {
            this.out.write((msg+"\n").getBytes());
            this.out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public void initGame(GamerRunnable game) {
    try {
        boolean messageLoop = true;

        String line1 = null;
        String line2 = null;

        while (messageLoop) {

            line1 = inGamer.readLine();

            if (line1 != null) {

                System.out.println("Gamer says: "+line1);

                GamerRunnable gamer2 = null;
                if (game.getName().equals("red")) {
                    gamer2 = GamerRunnable.getGameThreadByName("black");
                }
                else if (game.getName().equals("black")) {
                    gamer2 = GamerRunnable.getGameThreadByName("red");
                }

                if (gamer2 != null) {                       
                    System.out.println("Adding to Queue");
                    GamerRunnable.messageQueues.get(gamer2).offer(line1);
                }
            }

            line2 = GamerRunnable.messageQueues.get(game).poll();

            if (line2 != null) {
                //receiving from Queue
                System.out.println(line2);
                game.getGameInstance().sendToGamer(line2);      
            }
        }