Java线程无法按预期启动/工作

时间:2014-11-13 10:49:15

标签: java multithreading sockets networking

我正在实现一个简单的客户端 - 服务器架构,其中多个客户端应该能够连接到服务器,并且可以在服务器和客户端之间交换字符串。

我的想法是我将在每一方都有两个线程:一个监听器,不断检查输入流中是否有新的东西,还有一个写入线程,如果有要写的东西,它会写入套接字。

但是,第二个线程甚至没有启动...只显示第一个sysout。

    //start new thread to handle client input
    new Thread(
            new ServerWorker(clientSocket, this, this.getIdCounter())).start();
    System.out.println("server side listener started");

    //start new thread to handle client output
    new Thread(new ServerWorkerListener(clientSocket)).start();
    System.out.println("server side writer started");

以下是ServerWorker的一些代码:

public void run() {
    try {
        OutputStream output = clientSocket.getOutputStream();
        while (true) {
            // output.write(("Pling!\n\n").getBytes());

            for (Client tempClient : server.getClientList()) {
                if ((tempClient.getId() == this.id)
                        && tempClient.isShouldSend()) {

                    output.write((tempClient.getOutputStream() + "\n\n")
                            .getBytes());
                    tempClient.setInputStream("");
                    tempClient.setShouldSend(false);
                }
            }

        }
    } catch (IOException e) {
        System.out.println("Error in serverWorker");
        e.printStackTrace();
    }

}

我真的不知道我错过了什么......

整个ServerWorker:         公共类ServerWorker实现Runnable {

protected Socket clientSocket = null;
protected String serverText = null;
protected int id;
protected Server server;

public ServerWorker(Socket clientSocket, Server server,
        int id) {
    this.clientSocket = clientSocket;
    this.serverText = serverText;
    this.id = id;
    this.server = server;
}

public void run() {
    try {
        OutputStream output = clientSocket.getOutputStream();
        while (true) {
            // output.write(("Pling!\n\n").getBytes());

            for (Client tempClient : server.getClientList()) {
                if ((tempClient.getId() == this.id)
                        && tempClient.isShouldSend()) {

                    output.write((tempClient.getOutputStream() + "\n\n")
                            .getBytes());
                    tempClient.setInputStream("");
                    tempClient.setShouldSend(false);
                }
            }

        }
    } catch (IOException e) {
        System.out.println("Error in serverWorker");
        e.printStackTrace();
    }

}

}

整个ServerWorkerListener:

    public class ServerWorkerListener implements Runnable {

private BufferedReader input;
private Socket clientSocket;

public ServerWorkerListener(Socket clientSocket) {
    this.clientSocket = clientSocket;
    run();
}

@Override
public void run() {
    System.out.println("its running");
    try {

        BufferedReader in = new BufferedReader(new InputStreamReader(
                clientSocket.getInputStream()));

        while (true) {
            System.out.println("it's looping");
            String inputLine = null;
            if ((inputLine = in.readLine()) != null) {
                JOptionPane.showMessageDialog(null, inputLine, "InfoBox: "
                        + "Message from client",
                        JOptionPane.INFORMATION_MESSAGE);

            }
        }

    } catch (UnknownHostException e) {
        System.err.println("Don't know about client");
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to client");
        System.exit(1);

    }
}

}

1 个答案:

答案 0 :(得分:0)

您正在run()的构造函数中调用ServerWorkerListener

新线程应该调用run(),否则,因为它包含一个无限循环,它将永远不会从构造函数返回,因此永远不会调用Thread的构造函数,更不用说它的start方法。因此,从构造函数中删除run()调用应该可以解决问题。