while((s = br.readLine()) != null){
bw.write(getMobileFrom(s));
bw.newLine();
}上面是我写的一个代码的片段,现在的问题是,在不修改这代码的情况,怎样在这循环里添加一条语句,这样可以让这循环语句,比如每隔一分钟再循环读取下一个字符串,意思就是每个循环之间的执行时间要间隔一分钟,各位大哥支个招吧
bw.write(getMobileFrom(s));
bw.newLine();
}上面是我写的一个代码的片段,现在的问题是,在不修改这代码的情况,怎样在这循环里添加一条语句,这样可以让这循环语句,比如每隔一分钟再循环读取下一个字符串,意思就是每个循环之间的执行时间要间隔一分钟,各位大哥支个招吧
bw.write(getMobileFrom(s));
bw.newLine();
Thread.sleep(1000*60);
}
不过其实用java的定时器Timer获取更优雅一点,上面是让线程休眠60秒,效果一样
public static void main(String[] args) {
java.util.Timer t = new java.util.Timer(); t.schedule(new Task(), 0, 60000);
}
}class Task extends java.util.TimerTask {
...... @Override
public void run() {
String s;
if((s = br.readLine()) != null){
bw.write(getMobileFrom(s));
bw.newLine();
} else {
this.cancel();
}
}
}
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.
Timer 有计时器 建议用这个