整数不会递增?
好的,所以我正在进行客户端/服务器测试,我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
new Integer
正在客户端线程方法内创建一个全新的Integer
实例,该实例对调用者不可用。但是,您需要考虑主线程和客户端线程之间的同步。这可以通过对重要对象或类使用
synchronized
语句来实现,例如用于整数的java.util.concurrent.atomic.AtomicInteger
,如下所示:您需要考虑如何共享并发执行线程之间的数据。这也适用于
Vector
和JTextArea
参数。您应该根据需要使用synchronize
语句包装对players
和textArea
对象的访问。new Integer
is creating a brand-newInteger
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 asjava.util.concurrent.atomic.AtomicInteger
for integers as follows:You need to think about how to share data between concurrently executing threads. This applies also to the
Vector<Player>
andJTextArea
arguments. You should wrap accesses to theplayers
andtextArea
objects usingsynchronize
statements as appropriate.创建
ClientThread
后,在main
中增加玩家 ID。客户端线程不应该负责增加玩家 ID。这是
main
的职责,它创建客户端线程并为其提供 ID。Increment the player ID in
main
after creating theClientThread
.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.如果要在方法中操作整数,则需要将其封装在对象中。
阅读这篇文章以更好地理解
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
如果需要,请尝试使用
IntHolder
。Try use
IntHolder
if you need it.