BufferedReader的readLine()方法读取不到换行和回车时会阻塞!而不是返回null!

2022-06-23 12:08:20

在做一客户一线程通信的例子中见了一个大坑!
在这里插入图片描述
在这里插入图片描述
这是clientThread的里的接收客户端发来消息的一个while循环。第一次看这个循环时总觉得有问题。误以为当客户端发来一条消息时。clientThread里的这个while语句把这条消息读完之后,就会返回null,然后循环结束。那么问题来了?循环结束后,整个run()方法也就结束了。之后该线程对象就释放了?服务端是怎么做到第二次读取这个客户端发来的消息的?

经过调试发现,读取一条客户端消息后,该while循环并没有结束!因为该while是个死循环!readLine()在没有读取到换行符或回车符时。是不会返回的。而是处于阻塞状态。所以这个while在读取一条消息后。一直在readLine()上阻塞。当客户端发来下一条消息时。会继续下一次循环,等待读取下一条消息

顺便附上ClientThread的代码
public class clientThread implements Runnable{
private Socket socket;
private BufferedReader br;
private BufferedWriter bw;
private Integer clientNum;
private ServerUI serverUI;

public clientThread(Socket socket, Integer clientNum, ServerUI serverUI) {
    this.socket = socket;
    this.clientNum = clientNum;
    this.serverUI = serverUI;
}

@Override
public void run(){
    try {
        //Charset.forName("utf-8"),指定转换流编码方式
        this.br=new BufferedReader(new InputStreamReader(this.socket.getInputStream(),Charset.forName("utf-8")));   
        this.bw=new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream(),Charset.forName("utf-8")));
        
        String inStr=null;
        //?????????????????????循环等待?????
        //while死循环?readLine()会阻塞???
        //while(true){
        while((inStr=br.readLine())!=null){ //客户机不关闭,反复等待。接收客户端信息
        this.serverUI.getTxtArea().append("客户端编号:"+this.clientNum+"_"+this.socket.getRemoteSocketAddress()+"消息:"+inStr+"\n");
        this.bw.write(inStr);
        this.bw.newLine();
        this.bw.flush();
        }
        //}
    } catch (IOException e) {
        JOptionPane.showMessageDialog(this.serverUI,"异常信息:"+e.getMessage(),"异常错误",JOptionPane.ERROR_MESSAGE);
    }finally{
        try {
            this.bw.close();
            this.br.close();
            this.socket.close();
        } catch (IOException ex) {
            Logger.getLogger(clientThread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    }
}
  • 作者:指东针
  • 原文链接:https://blog.csdn.net/wdw66666/article/details/105892899
    更新时间:2022-06-23 12:08:20