class BeeperControl {
private final static ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public static void main(String[] args){
beepForAnHour();
}
public static void beepForAnHour() {
final Runnable beeper = new Runnable() {
public void run() {
System.out.println(「beep」);
}
};
final ScheduledFuture beeperHandle =
scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
scheduler.schedule(new Runnable() {
public void run() {
beeperHandle.cancel(true);
}
}, 60 * 60, SECONDS);
}
}
此代碼每10秒鐘蜂鳴一小時。我如何讓它每10分鐘一次嗶嗶聲。因爲它應該每10分鐘一直保持蜂鳴聲(不只是一小時)將代碼更改爲永久重複
將它包裹在'while(true){}'裏面? – 11684 2012-07-25 09:11:27
第1步:以可讀方式格式化您的代碼。 – 2012-07-25 09:11:41
哪個更好.. schedulor或sleep ????????? – user1545477 2012-07-25 09:17:50