The wire does not function as a wire

advertisements

I've got some weird (or at least weird for me) problem with a thread. I've got main like this

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");

where ConnectionManager is

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

}
}

But even though my connectionManager is a thread it doesn't go to the System.out.println unless the connection is closed from the server. I've been thinking about this for some time now and I can't figure out why it doesn't work.

Don't think that matters but main and connectionManager are in other maven modules (main is in a GUI module which has dependency on ConnectionManager's module)

I would appreciate any advises.


Calling connectionManager.run() does not spawn the thread. It merely calls run and waits until it's done to reach the System.out line.

To run the run method in a separate thread, you'll need to call connectionManager.start(). See https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#start().