连接到服务器后,我在服务器上运行一些命令,然后尝试将服务器知识用于控制台;
while(i!=-1){
String c="";
String line = "";
try {
while ((i = input.read()) != 10 && i!=-1) {
bx[0] = (byte) i;
c = new String(bx);
line = line + c ;
System.out.print(c);
}
} catch (IOException e2) {
e2.printStackTrace();
}
File outfile = new File("calltrak.txt");
boolean append = true;
try
{
if (!outfile.exists())
{
append = false;
}
FileWriter fout1 = new FileWriter("calltrak.txt",append);
PrintWriter fileout = new PrintWriter(fout1,true);
fileout.println(line);
fileout.flush();
fileout.close();
} catch (IOException e1) {
e1.printStackTrace();
}
disp.append(line);
}
但问题是,当程序从服务器窗口读取所有行时,在服务器中它等待新的输入,而我的程序仍然尝试读取该行,因此它被锁定...我该如何解决这个问题...(注意:使用计时器不是解决问题的方法,因为程序读取的行可以是 100 行或 100000 行,有时服务器可能会运行缓慢)(在代码中“disp”是 Jpanel 名称)
最佳答案
我使用并行线程解决了这个问题。通过启动 Inputstream read 方法,我还启动了另一个线程并在其中放入一个计时器。如果 read 方法等待超过 5 秒,则其他线程 sen -1 到第一个循环,因此循环终止。
关于java - input.read() 函数。锁定 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7109444/