开发者

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.

 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();
      开发者_Go百科      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.

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.


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.


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


Try use IntHolder if you need it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜