线程不能用作线程

Raf*_*iak -2 java multithreading

我对线程有一些奇怪的问题(或者至少对我来说很奇怪).我有这样的主要

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");
Run Code Online (Sandbox Code Playgroud)

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();
    }

}
}
Run Code Online (Sandbox Code Playgroud)

但即使我的connectionManager是一个线程,它也不会转到System.out.println,除非从服务器关闭连接.我已经考虑了一段时间了,我无法弄清楚为什么它不起作用.

不要认为重要但main和connectionManager在其他maven模块中(主要在GUI模块中,它依赖于ConnectionManager的模块)

我很感激任何建议.

Rea*_*tic 5

如果run()直接运行该方法,则实际上并未在新线程中运行它.

要实际启动线程来执行任务,您必须运行其start()方法.在引擎盖下,它创建一个本机线程并将控制传递给它,而另一个线程是运行该run()方法的线程.