我在java中使用ArrayList时遇到了一些问题,我正在创建一个新的类对象,给出这些值,将该对象添加到arrayList,然后对已使用的对象使用关键字“new”,使用它与不同的数据,但未设置但之前设置的数据仍然设置?
这是我的代码
private static ArrayList<PongServerThread> games = new ArrayList<PongServerThread>();
private static int players = 0;
private static PongServerThread game;
public static void main( String args[] ) throws IOException, InterruptedException
{
while(true)
{
System.out.println("Start of Loop");
if(game.getPlayers() == 0)
{
System.out.println("Waiting for players");
}
if(game.getPlayers() == 2)
{
System.out.println("Game Added");
games.add(game);
games.get((games.size())-1).start();
game.setPlayers(0);
}
game = new PongServerThread();
}
}
这段代码运行正常,但在我的PongServerThread中,一旦两个客户端连接,main将启动该线程,并且需要启动一个新的PongServerThread对象,因此另外两个客户端可以连接到该线程,但是发生了什么,一旦线程启动2个客户端,这里的新线程
game = new PongServerThread();
这将再次创建,但是当新客户端连接时,它将接管第一个客户端,如果另一个客户端连接,它将接管第二个客户端,依此类推,所以发生的事情是
的行game = new PongServerThread();
是不是真的在制作新的PongServerThread,我该如何解决这个问题呢?这也是2个客户端已连接时的控制台输出,然后是另外1个连接
Start of Loop
Waiting for players
Pong
400.0:301.0:60.0:300.0:740.0:300.0
Server started
Server was setup and will try to create a socket
Client Connected with ID:0
Checking readLine value
Client Connected with ID:1
Start of Loop
Game Added
Pong
400.0:300.0:60.0:300.0:740.0:300.0
Server started
Server was setup and will try to create a socket
Checking readLine value
Client Connected with ID:0
Checking readLine value
Ryan更新-----
这是我的服务器构造函数
public PongServerThread() throws IOException, InterruptedException
{
//Setup
super("PongServerThread");
setupPong();
boolean listen = false;
boolean playing = false;
System.out.println(rtnInfo());
System.out.println("Server started");
//Create a serverSocket to listen on
ServerSocket serverSocket = null;
try
{
serverSocket = new ServerSocket(port);
listen = true;
System.out.println("Server was setup and will try to create a socket");
}
catch(IOException e)
{
System.err.println("Could not listen on port:" + port);
System.exit(1);
}
while(listen)
{
/*PongPlayerThread player1 = */
players[idPlayer] = new PongPlayerThread(serverSocket.accept(), idPlayer, rtnInfo());
players[idPlayer].start();
System.out.println("Client Connected with ID:" + idPlayer);
players[0].passData(rtnInfo());
idPlayer++;
if(idPlayer > 1)
{
listen = false;
playing = true;
}
}
int timer = 0;
System.out.println("Server Closed");
serverSocket.close();
}
--- ---更新
我刚注意到我正在声明一些变量是静态的,这很可能是我收到错误的原因,但现在当我尝试运行我的服务器代码时
class Main
{
// Variable
private ArrayList<PongServerThread> games = new ArrayList<PongServerThread>();
private int players = 0;
private PongServerThread game;
public void main( String args[] ) throws IOException, InterruptedException
{
//PongServerThread game = new PongServerThread();
/*if(pt.getPlayers() == 2)
{
System.out.println("Started");
pt.start();
}*/
PongServerThread game = new PongServerThread();
while(true)
{
games.add(game);
System.out.println("Game Added");
game.start();
game = new PongServerThread();
}
/*System.out.println("Pong");
PongModel model = new PongModel();
PongView view = new PongView();
new PongController( model, view );
model.addObserver( view ); // Add observer to the model
view.setVisible(true); // Display Screen
model.makeActiveObject(); // Start play*/
}
}
我收到此错误
Could not get Input/Output from server
有谁知道为什么我会这样做?如果需要,我可以粘贴更多代码。
答案 0 :(得分:1)
要启动一个帖子,你必须打电话,例如:
game.start();
一般来说,最好实施Runnable
,然后说:
Runnable myThread = ...;
new Thread(myThread).start();
答案 1 :(得分:0)
因为PongServerThread()仅在有2个玩家的游戏时返回,所以你可以非常简化主要功能:
{
PongServerThread game = new PongServerThread();
while(true)
{
games.add(game);
System.out.println("Game Added");
game.start();
game = new PongServerThread();
}
}
线game.setPlayers(0);
非常奇怪:为什么将游戏的玩家数量设置为零,当游戏刚刚开始时有两个玩家。
编辑:
如果我理解正确,setPlaysers(0)
调用将静态变量players
设置为零。这就是第三个客户端获得ID = 0的原因;第四个客户得到ID = 1;
也许这让它看起来像'覆盖'。
因此删除game.setPlayers(0);
将解决此问题,所有玩家都将获得唯一标识符。