我對線程有一些奇怪的(或至少是怪異的)問題。我有主這樣線程不能作爲線程
public class Main {
public static void main(String[] args) {
int port = 5555;
String server = "127.0.0.1";
int x=20, y=20, squareSize = 25;
Thread connectionManager = new ConnectionManager(server, port, x, y);
connectionManager.run();
System.out.print("BUM");
其中的ConnectionManager是
public class ConnectionManager extends Thread{
private String server;
private int port;
private int x;
private int y; //dlugosc mapy
private Socket socket;
private int[][] currentMap;
File file;
PrintWriter printWriter;
public ConnectionManager(String server, int port, int x, int y) {
this.server = server;
this.port = port;
this.x = x;
this.y = y;
currentMap = new int[x][y];
}
public int[][] getCurrentMap() {
return currentMap;
}
@Override
public void run() {
try {
socket = new Socket(server, port);
DataInputStream is = new DataInputStream(socket.getInputStream());
while (true) {
if (is.readBoolean() == true)
{
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
currentMap[i][j] = is.readInt();
}
}
}
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
但是,即使我的ConnectionManager是一個線程,除非連接從關閉它不會去到的System.out.println服務器。我已經考慮了一段時間了,我無法弄清楚爲什麼它不起作用。
不要以爲事情,但主的ConnectionManager在其他的Maven模塊(主要是其具有的ConnectionManager的模塊依賴的GUI模塊)
我將不勝感激的任何建議。
我不能相信人們正在upvoting經典開始vs反覆運行錯誤... –