简体   繁体   English

在多线程环境中无法通过等待和通知从套接字输入流中读取

[英]Unable to read from the Socket Input Stream in a multithreaded environment with wait and notify

I am using 2 threads- ReaderThread for reading from the Socket input stream and the WriterThread for writing to the socket output stream. 我正在使用2个线程-ReaderThread用于从Socket输入流读取,而WriterThread用于写入套接字输出流。 Both of them work fine when am just writing to the stream and not reading from the stream. 仅在写入流中而不从流中读取时,它们两个都可以正常工作。 But when am also Reading from the input stream the program doesn't run further, it hangs. 但是,当我也在从输入流中读取时,程序不再运行,则挂起。

Below is the code which does the writing- Its in the WriterThread class. 下面是执行WriterThread类中的代码。

try{
        Scanner consoleReader = new Scanner(System.in);
        String inst="";
        PrintWriter writer = new PrintWriter(client.getOutputStream(),true);
        while(true){
                synchronized(response){


                    System.out.print("Enter something: ");
                    System.out.flush();

                    inst=consoleReader.nextLine();
                    System.out.println(inst);
                    instruction.setInstruction(inst);
                    if(!instruction.getInstruction().equals("")){

                        writer.print(instruction.getInstruction());
                        writer.flush();
                        response.notifyAll();   

                        if(instruction.getInstruction().equalsIgnoreCase("end")){
                            break;
                        }
                        response.wait();

                    }
                }//End of response sync
        }//End of while
        }catch(IOException ioex){
            System.out.println("IO Exception caught in Writer Thread");
        }catch(InterruptedException iex){
            System.out.println("Writer thread interrupted");
        } 

The above code- Reads from the command line, writes it to the socket outputstream using the "writer" object. 上面的代码-从命令行读取,并使用“ writer”对象将其写入套接字输出流。

Below is the code which reads from the stream- Its in the ReaderThread class 下面是从流中读取的代码-在ReaderThread类中

try{        
        Scanner reader = new Scanner(client.getInputStream());
        String txtRead="";
        outer:
        while(true){
            synchronized(response){
                response.wait();
                System.out.println("Reader Running");

                while(reader.hasNext()){ //Line-Beg
                    txtRead=reader.next();
                    System.out.println("Received: "+txtRead);
                    if(txtRead.equalsIgnoreCase("end")){
                        response.notifyAll();
                        break outer;
                    }
                }//End of reader while, Line-End

                response.notifyAll();
            }//End of response sync
        }//End of while
        }catch(IOException ioex){
            System.out.println("IOException caught in ReaderThread");
        }
        catch(InterruptedException iex){
                System.out.println("Interrupted ReaderThread");
        }  

The above code does the reading from the Socket's input stream. 上面的代码从Socket的输入流中读取数据。 The problem wit the above code is it waits indefinitely after printing- "Reader Running". 上面的代码的问题是它在打印“ Reader Running”后无限期等待。 But when i comment out the code from Line-Beg to Line-End it executes properly giving chance for the other WriterThread to write to the output stream. 但是,当我注释掉从Line-Beg到Line-End的代码时,它会正确执行,从而为其他WriterThread写入输出流提供了机会。 Why is it so? 为什么会这样呢?

Note: "response" is a common object which is being used for synchronization. 注意:“响应”是用于同步的常见对象。 Also am closing the "server socket" once the main method completed execution. 主方法完成执行后,还要关闭“服务器套接字”。 Also the client socket is a "C" socket. 客户端套接字也是“ C”套接字。

I dont see a problem with the notify() and wait() methods being used. 我看不到使用notify()和wait()方法的问题。

reader.hasNext() blocks while holding the response object lock until some input is available in the socket input stream. reader.hasNext()在保持response对象锁定的同时阻塞,直到套接字输入流中的某些输入可用为止。 This prevents the writer thread from taking the response lock and writing to the socket output stream. 这样可以防止编写器线程获取response锁并写入套接字输出流。 Why are you synchronizing the reader and writer threads on the same lock? 为什么要在同一锁上同步读取器线程和写入器线程? It seems unnecessary to me. 对我来说似乎没有必要。

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

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