似乎Java编译器(Jdk8)缺少类似'j = j ++;'的分配的警告。无效,但会为“ j = ++ j;”等赋值生成警告 确实有效。我已附上一个脚本进行演示。
请注意,您必须选择适当的标志来报告Java编译器Java编译器设置内的分配错误
public static void main(String[] args) {
int j = 0;
j = ++j; //Compiler warning: The assignment to variable j has no effect
System.out.println("j="+j); //Prints 1
int i = 0;
i = i++; //Compiler warning: 'None'
System.out.println("i="+i); //Prints 0
}
Run Code Online (Sandbox Code Playgroud)
这是Java编译器的错误吗?
如何在设置超时值时获取进程的输出?
我目前正在使用 apache commons io utils 从进程的标准和错误输出创建一个字符串。
下面的代码按原样(带有注释)适用于终止的进程。但是,如果进程没有终止,主线程也不会终止!
如果我取消注释掉已注释的代码,而是注释掉 process.waitfor(),该方法将正确销毁非终止进程。但是,对于终止进程,无法正确获得输出。似乎一旦 waitfor() 完成,我就无法获得进程的输入和错误流?
最后,如果我尝试将注释部分移动到 process.waitfor() 当前所在的位置,删除 process.waitfor() 并取消注释注释部分,那么对于非终止进程,主线程也不会停止。这是因为 process.waitfor(15, ...) 永远不会到达。
private static Outputs runProcess(String command) throws Exception {
Process process = Runtime.getRuntime().exec(command);
// if (!process.waitFor(15, TimeUnit.SECONDS)) {
// System.out.println("Destroy");
// process.destroy();
// }
// Run and collect the results from the standard output and error output
String stdStr = IOUtils.toString(process.getInputStream());
String errStr = IOUtils.toString(process.getErrorStream());
process.waitFor();
return new Outputs(stdStr, errStr);
}
Run Code Online (Sandbox Code Playgroud)