如何在java中以编程方式停止无限循环?
这里我们将一个java程序作为另一个java程序的输入。如果给定的程序包含任何无限循环,那么我想终止输入程序的特定进程。在代码中,我们也使用线程来停止进程,但我们没有得到任何解决方案如何在java中以编程方式停止无限循环?,java,Java,这里我们将一个java程序作为另一个java程序的输入。如果给定的程序包含任何无限循环,那么我想终止输入程序的特定进程。在代码中,我们也使用线程来停止进程,但我们没有得到任何解决方案 public class JavaCompiler extends Thread { String path = ""; String fname = ""; String output = ""; String totalPath = ""; String s = nu
public class JavaCompiler extends Thread {
String path = "";
String fname = "";
String output = "";
String totalPath = "";
String s = null;
String log = "";
String errorfile = "";
String ret = "";
private String resultString = "";
@Override
public void run() {
System.out.println("Entered RUN method");
try {
System.out.println("sleep Run Method");
FileOutputStream fos = new FileOutputStream(errorfile, true);
System.out.println("sleep Run Method1");
Process pro = Runtime.getRuntime().exec(totalPath);
System.out.println("sleep Run Method2");
BufferedReader stdError = new BufferedReader(new InputStreamReader(pro.getErrorStream()));
System.out.println("Entered stderror");
boolean error = false;
log += "\n....\n";
while ((s = stdError.readLine()) != null) {
error = true;
fos.write(s.getBytes());
System.out.println("Error Errorloop");
ret = "errortrue";
}
fos.close();
if (error == false) {
System.out.println("output loop");
output = runProcess(path, fname);
ret = output;
}
if (output.equals("success")) {
System.out.println("Output success loop");
ret = "success";
} else {
System.out.println("Output false loop");
ret = "errortrue";
}
} catch (Exception e) {
System.out.println("Message"+e.getMessage());
ret = "error";
}
}
public String javaProgram(String strFilePath, String filename) throws Exception {
path = strFilePath;
fname = filename;
totalPath = "javac" + " " + strFilePath + "/" + fname + ".java";
System.out.println("Total path" + totalPath);
errorfile = strFilePath + "/" + fname + "_Error.txt";
// JavaCompiler j = new JavaCompiler();
// j.start();
start();
Thread.sleep(2000);
//j.stop();
System.out.println("Stopping run method");
System.out.println("After sleep");
System.out.println("Returing value=====" + ret);
return ret;
}
private static String runProcess(String command, String filename) throws Exception {
String path1 = "java" + " " + "-cp" + " " + command + "/" + " " + filename;
System.out.println("Path" + path1);
String filepath = command + "/" + filename + "_output.txt";
System.out.println("filepath=============" + filepath);
System.out.println("Entered");
Process pro = Runtime.getRuntime().exec(path1);
String s = printLines(filepath, pro.getInputStream());
return s;
}
private static String printLines(String name, InputStream ins) throws Exception {
String line = null;
int count = 0;
String strFilePath = name;
System.out.println("NAME" + name);
FileOutputStream fos = new FileOutputStream(strFilePath, false);
BufferedReader in = new BufferedReader(new InputStreamReader(ins));
while ((line = in.readLine()) != null) {
fos.write(line.getBytes());
System.out.println(name + " " + line);
// count++;
// if(count>10){
// break;
// }
}
fos.close();
return "success";
}
/**
* @return the resultString
*/
}
我认为如果你发布更少的代码,你就更有可能得到答案。向我们展示一个小示例,说明您正在尝试执行的操作。Process pro=Runtime.getRuntime().exec(path1)此进程执行java程序,如果此程序包含任何无限循环,我希望在特定时间段后终止该进程。