20 线程中添加超时的方法
isExecSuccess=false; exitCode=-1; Thread newThread=new Thread(new Runnable() { @Override public void run() { try { exitCode= process.waitFor(); isExecSuccess=true; } catch (InterruptedException e) { e.printStackTrace(); } } }); newThread.start(); long startTime=System.currentTimeMillis(); long currentTime; long timeOut=0; while (timeOut< this.ftpCommandTimeout && !isExecSuccess){ Thread.sleep(10); currentTime=System.currentTimeMillis(); timeOut=currentTime-startTime; } if(newThread.isAlive()){ newThread.interrupt(); }
isExecSuccess=false;
exitCode=-1;
Thread newThread=new Thread(new Runnable() {
@Override
public void run() {
try {
exitCode= process.waitFor();
isExecSuccess=true;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
newThread.start();
long startTime=System.currentTimeMillis();
long currentTime;
long timeOut=0;
while (timeOut< this.ftpCommandTimeout && !isExecSuccess){
Thread.sleep(10);
currentTime=System.currentTimeMillis();
timeOut=currentTime-startTime;
}
if(newThread.isAlive()){
newThread.interrupt();
}
本文来自博客园,作者:life_start,转载请注明原文链接:https://www.cnblogs.com/yangh2016/p/6000871.html