简体   繁体   English

一个Java线程object怎么能从原来的class调用一个方法?

[英]How can a Java thread object call a method from the original class?

I have a client-server chat program, where the server will create aa new thread for each accepted connection, where NewClient extends Thread to handle requests from a specific client.我有一个客户端-服务器聊天程序,服务器将为每个接受的连接创建一个新线程,其中 NewClient 扩展 Thread 以处理来自特定客户端的请求。

Here is a snippet of the server code:这是服务器代码的片段:

public void startServer() {
    // thread to handle requests
    new Thread(() -> {
        try (ServerSocket serverSocket = new ServerSocket(port)) {
            System.out.println("Server started at " + new Date());

            while (true) {
                // listen for connection request
                Socket socket = serverSocket.accept();

                // increment total clients connected
                clientCount++;

                // create new client
                NewClient newClient = new NewClient(socket, clientCount);

                // add client to active client list
                activeClientList.add(newClient);

                // start thread
                newClient.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }).start();
}

In NewClient, the thread will loop and check the input stream.在 NewClient 中,线程将循环并检查输入 stream。 If an input stream is detected, I want it to broadcast the message in the input stream to all clients.如果检测到输入 stream,我希望它将输入 stream 中的消息广播给所有客户端。

Here is the run method of NewClient:这是NewClient的run方法:

public void run() {
    while(true) {
        try {
            // read from client
            Data message = (Data) objectInputStream.readObject();
            System.out.println("Received " + message.text + " from " + message.name);

            // broadcast to all clients
            // broadcastMessage(message)


        } catch (IOException | ClassNotFoundException e) {
            System.out.println("Failed to get input stream");
            e.printStackTrace();
        }
    }
}

Back in the server class, there is a method called broadcastMessage(Data data), which will loop through all connected clients in an array list and use the the output streams to send the data to each client.回到服务器 class 中,有一个名为 broadcastMessage(Data data) 的方法,它将遍历数组列表中所有连接的客户端,并使用 output 流将数据发送到每个客户端。

Here is the method:这是方法:

public synchronized void broadcastMessage(Data data) {
    Integer size = activeClientList.size();

    // loop through all clients
    for(int i=size-1;i>=0;i--) {
        NewClient client = activeClientList.get(i);

        if(!client.writeToClient(data)) {
            activeClientList.remove(i);

            // notify users of disconnect
            Data update = new Data(data.name, data.name + " has disconnected.");
            broadcastMessage(update);
        }
    }
}

I understand that if this was an anonymous thread, I can call the broadcast method from within the server.我知道如果这是一个匿名线程,我可以从服务器内部调用广播方法。 How can I call this method within a thread class?如何在线程 class 中调用此方法?

Thank you in advanced for your feedback.提前感谢您的反馈。

// create new client
NewClient newClient = new NewClient(socket, clientCount, this);

Thank you Johannes Kuhn for pointing out that the server can be passed into the thread constructor to call the method.感谢 Johannes Kuhn 指出可以将服务器传递给线程构造函数来调用该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM