1 回答
TA贡献1836条经验 获得超5个赞
您可以使用java.util.function.Consumer。将消费者实例传递给客户端构造函数并accept在接收时调用消费者的方法:
public class Client {
private final Consumer<String> messageConsumer;
public Client( String ip, int port, Consumer<String> messageConsumer) throws IOException {
this.messageConsumer = messageConsumer;
Thread recievingRoutineThread = new Thread( new Runnable() {
@Override
public void run() {
while(true) {
String recievingString = null;
try{ recievingString = recieve();
}catch(Exception ex) {}
if(recievingString != null && recievingString != "")
sendRecievedMessageOutside(recievingString);
}
}
} );
recievingRoutineThread.start();
}
private void sendRecievedMessageOutside(String message){
messageConsumer.accept( message );
}
}
- 1 回答
- 0 关注
- 133 浏览
添加回答
举报