我想从输入流套接字读取多行。
public void sendAsync(HashSet<Socket> socketHashset, Vector<String> vectors,
PrintWriter printwriter) throws IOException, InterruptedException {
ExecutorService service = Executors.newSingleThreadExecutor();
service.execute(() -> {
for(Socket v : socketHashset){
//this.printwriter = new PrintWriter(v.getOutputStream(),true);
for(String str: vectors ){
this.printwriter.println(str);
this.printwriter.flush();
}
}
});
/* Receiver */
while(true){
try {
printWriter.println(keybordScanner.nextLine())
while(scannerBuffer.hasNextLine()){
System.out.println(scannerBuffer.nextLine());
}
} catch(NoSuchElementException e){
e.printStackTrace();
}
}
}
问题是编译器阻塞然后检查“hasNextLine”
while(scannerBuffer).hasNextLine())
我如何解决这个问题?一些替代方案。
答案 0 :(得分:0)
Thread readingThread=new Thread(new readFromSock(scannerBuffer));
readingThread.start();
并制作另一个课程:
class readFromSock implements Runnable{
Scanner scannerBuffer;
public readFrom(Scanner scan){
scannerBuffer=scan;
}
public void run(){
try {
while(scannerBuffer.hasNextLine()){
System.out.println(scannerBuffer.nextLine());
}
} catch(NoSuchElementException e){
e.printStackTrace();
}
}
}