使用ProcessBuilder从JAVA中运行外部C代码。 (编译好的)C代码需要大约1秒才能产生输出 - 相对较慢。
我注意到,在C代码完成之前,BufferedReader Readline似乎非常耗费CPU。由于我必须每5秒运行一次外部C代码,因此CPU利用率会增加。这是readline代码:
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(in));
String line = null;
while ( (line = br.readLine()) != null) {
this.pw.add(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
我可以发布整个代码 - 但是,它与您在this page上看到的内容非常相似。
Here is the JProfiler screenshot.
有什么建议吗?