我在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 个客户端可以连接到该线程,但发生的是,一旦 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
瑞安的更新-----
这是我的服务器构造函数
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
有人知道我为什么会得到那个吗?如果需要,我可以粘贴更多代码。