【发布时间】:2010-01-29 15:22:42
【问题描述】:
我使用 2 个线程 - ReaderThread 用于读取 Socket 输入流,而 WriterThread 用于写入套接字输出流。当我只是写入流而不是从流中读取时,它们都可以正常工作。但是,当我还从输入流中读取时,程序不会进一步运行,它会挂起。
下面是编写的代码——它在 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");
}
以上代码- 从命令行读取,使用“writer”对象将其写入套接字输出流。
下面是从流中读取的代码——它在 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");
}
上面的代码从 Socket 的输入流中读取数据。上面代码的问题是它在打印后无限期地等待 - “Reader Running”。但是当我注释掉从 Line-Beg 到 Line-End 的代码时,它会正确执行,让其他 WriterThread 有机会写入输出流。为什么会这样?
注意:“响应”是用于同步的通用对象。一旦主要方法完成执行,我也会关闭“服务器套接字”。客户端套接字也是“C”套接字。
我没有发现正在使用的 notify() 和 wait() 方法有问题。
【问题讨论】:
标签: java multithreading sockets