我读了这个
您如何知道该方法是成功完成还是被中断?
编辑:为了使问题更加清晰和具体。下面是我的代码..我想执行 test.java 文件并获取它的运行时..但是如果需要超过 1 秒,我想显示一条错误消息并停止它本身就在那里..
public class cl {
public static void main(String args[])throws IOException
{
String s=null;
Process p=Runtime.getRuntime().exec("javac C:\\Users\\Lokesh\\Desktop\\test.java");
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
Timer timer=new Timer(true);
InterruptTimerTask interruptTimerTask=new InterruptTimerTask(Thread.currentThread());
timer.schedule(interruptTimerTask,1);
try{
Runtime.getRuntime().exec("java C:\\Users\\Lokesh\\Desktop\\test");
}
catch (Exception e)
{
e.printStackTrace();
}
finally {
timer.cancel();
}
}
static class InterruptTimerTask extends TimerTask {
private Thread thread;
public InterruptTimerTask(Thread thread)
{
this.thread=thread;
}
@Override
public void run()
{
thread.interrupt();
}
}
}