java多線程並發executorservice(義務調劑)類。本站提示廣大學習愛好者:(java多線程並發executorservice(義務調劑)類)文章只能為提供參考,不一定能成為您想要的結果。以下是java多線程並發executorservice(義務調劑)類正文
package com.yao;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
/**
* 以下是一個帶辦法的類,它設置了 ScheduledExecutorService ,2秒後,在 1 分鐘內每 10 秒鐘蜂鳴一次
*
* @author yaokj
*
*/
class BeeperControl {
private final ScheduledExecutorService scheduler = Executors
.newScheduledThreadPool(1);
public void beepForAnHour() {
final Runnable beeper = new Runnable() {
public void run() {
System.out.println("beep");
}
};
final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate(
beeper, 2, 10, TimeUnit.SECONDS);
scheduler.schedule(new Runnable() {
public void run() {
beeperHandle.cancel(true);
scheduler.shutdown();
}
}, 60, TimeUnit.SECONDS);
}
}
public class ScheduledExecutorServiceDemo {
public static void main(String[] args) {
new BeeperControl().beepForAnHour();
}
}