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();
}
posted @ 2016-10-26 16:29  life_start  阅读(321)  评论(0编辑  收藏  举报