整数不会递增?

发布于 2024-09-24 23:24:41 字数 1640 浏览 4 评论 0原文

好的,所以我正在进行客户端/服务器测试,我将 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);
    }

这是它在线程中递增的位置:

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;
    }

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.

 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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝眼睛不忧郁 2024-10-01 23:24:41

new Integer 正在客户端线程方法内创建一个全新的 Integer 实例,该实例对调用者不可用。

但是,您需要考虑主线程和客户端线程之间的同步。这可以通过对重要对象或类使用 synchronized 语句来实现,例如用于整数的 java.util.concurrent.atomic.AtomicInteger ,如下所示:

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.
  }
}

您需要考虑如何共享并发执行线程之间的数据。这也适用于 VectorJTextArea 参数。您应该根据需要使用 synchronize 语句包装对 playerstextArea 对象的访问。

new Integer is creating a brand-new Integer instance inside the client thread method which is not available to the caller.

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:

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. You should wrap accesses to the players and textArea objects using synchronize statements as appropriate.

不醒的梦 2024-10-01 23:24:41

创建 ClientThread 后,在 main 中增加玩家 ID。

客户端线程不应该负责增加玩家 ID。这是 main 的职责,它创建客户端线程并为其提供 ID。

Increment the player ID in main after creating the ClientThread.

The client thread should not be responsible for incrementing the player ID. This is the responsibility of main, which is the one creating client threads and giving them their IDs.

时间海 2024-10-01 23:24:41

如果要在方法中操作整数,则需要将其封装在对象中。

阅读这篇文章以更好地理解
http://www.javaworld.com/javaworld /javaqa/2000-05/03-qa-0526-pass.html

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

你好,陌生人 2024-10-01 23:24:41

如果需要,请尝试使用 IntHolder

Try use IntHolder if you need it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文