50,530
社区成员
发帖
与我相关
我的任务
分享
public class doWrite extends TimerTask {
public doWrite() {
}
String value;
Timer myTimer = new Timer();
// public doWrite(String str) { //测试
// this.str = str;
// }
public void doWrite( String value ) {
try {
// out.println(value); //
// out.flush(); //写入到刷新 , 执行同步
System.out.println( value );
}
catch( Exception e ) {
e.printStackTrace();
}
}
public void run() {
// doWrite(value);
}
public static void main(String args[]) {
doWrite myTask = new doWrite();
// long beginTime = System.currentTimeMillis();
myTask.start(0,15); //180秒 ... 15s test
boolean finished = true;
try{
while(finished){
Thread.sleep(5000);
// long endTime = System.currentTimeMillis(); //(endTime - beginTime) > 15000
if(value.indexof(">") > 0){ //如何判断dos执行结束? 判断任务已经完成,完成了,停止计时,继续执行下一个任务
finished = false;
System.out.println("--- false ---");
myTask.end();
}
}
}
catch(Exception e){
e.printStackTrace();
}
System.out.println("---disconnect---");
myTask.end(); //程序结束时用cancel()结束Timer
}
public void start(int delay, int internal) {
myTimer.schedule(this, delay * 1000, internal * 1000); //利用timer.schedule方法
}
public void end(){
myTimer.cancel();
}
}