简体   繁体   English

整数不会增加?

[英]Integer won't increment?

Okay, so I have a client/server test going on, and I am passing the Integer playerID to a thread where it gives the int value to a simple Player object, than increments playerID by 1. 好的,我正在进行客户端/服务器测试,我将Integer playerID传递给一个线程,该线程将int值赋予一个简单的Player对象,而不是将playerID递增1。

 public static void main(String[] args) throws IOException {

        Vector<Player> player = new Vector<Player>();

        SlickServer ss = new SlickServer();
        ss.setVisible(true);

        ServerSocket serverSocket = new ServerSocket(4444);
        boolean listening = true;

        Integer playerID = new Integer(0);

        while(listening){
            ss.textArea.append("Waiting to connect with player: " + playerID.intValue()  + "\n");
            new ClientThread(serverSocket.accept(), player, playerID, ss.textArea).start();
            ss.textArea.append("Waiting to connect with player: " + playerID.intValue() + "\n");
        }

        serverSocket.close();
        System.exit(0);
    }

and here's where it increments it in the thread: 这是它在线程中递增的地方:

public ClientThread(Socket acceptedSocket, Vector<Player> players, Integer playerID, JTextArea textArea){
        super("ClientThread");
        this.acceptedSocket = acceptedSocket;
        this.players = players;
        players.add(new Player(50,50, playerID.intValue()));

        if(players != null)
            System.out.println("Not Null: " + players.size());

        boolean b = false;
        for(int i = 0; i < players.size(); i++){
            if(!b){
                if(players.get(i).id == playerID){
                    me = players.get(i);
                    b = true;
                }
            }
        }

        playerID = new Integer(playerID.intValue() + 1);
        this.textArea = textArea;
    }

new Integer is creating a brand-new Integer instance inside the client thread method which is not available to the caller. new Integer正在客户端线程方法内创建一个全新的Integer实例,该实例对调用者不可用。

However, you need to consider synchronization between the main and client thread. 但是,您需要考虑主线程和客户端线程之间的同步。 This can be achieved using synchronized statements for nontrivial objects or classes such as java.util.concurrent.atomic.AtomicInteger for integers as follows: 这可以通过对非平凡对象或类(例如java.util.concurrent.atomic.AtomicInteger的整数使用synchronized语句来实现,如下所示:

AtomicInteger playerID = new AtomicInteger(0);
while (listening) {
  ss.textArea.append("Waiting to connect with player: " + playerID.get()  + "\n");
  new ClientThread(serverSocket.accept(), player, playerID, ss.textArea).start();
  ss.textArea.append("Waiting to connect with player: " + playerID.get() + "\n");
}

class ClientThread {
  public ClientThread(Socket acceptedSocket, Vector<Player> players, AtomicInteger playerID, JTextArea textArea) {
    // etc.
    playerID.incrementAndGet();
    // etc.
  }
}

You need to think about how to share data between concurrently executing threads. 您需要考虑如何在并发执行的线程之间共享数据。 This applies also to the Vector<Player> and JTextArea arguments. 这也适用于Vector<Player>JTextArea参数。 You should wrap accesses to the players and textArea objects using synchronize statements as appropriate. 你应该换行访问,以playerstextArea使用对象synchronize陈述合适。

Increment the player ID in main after creating the ClientThread . 创建ClientThread后,在main增加玩家ID。

The client thread should not be responsible for incrementing the player ID. 客户端线程不应该负责增加玩家ID。 This is the responsibility of main , which is the one creating client threads and giving them their IDs. 这是main的责任,即创建客户端线程并为其提供ID的职责。

如果需要,请尝试使用IntHolder

If you want to manipulate the integer within the method you need to encapsulate it within an object. 如果要在方法中操作整数,则需要将其封装在对象中。

Read this article for a better understanding http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html 阅读本文以获得更好的理解http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM