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();
}
}
您可能感兴趣的文章:
- java多线程开发ScheduledExecutorService简化方式
- java多线程CountDownLatch与线程池ThreadPoolExecutor/ExecutorService案例
- Java使用ExecutorService来停止线程服务
- 在spring boot中使用java线程池ExecutorService的讲解
- Java ExecutorService四种线程池使用详解
- 详解JDK中ExecutorService与Callable和Future对线程的支持
- 详解Java利用ExecutorService实现同步执行大量线程
- 理解java多线程中ExecutorService使用
- Java 线程池ExecutorService详解及实例代码
- ExecutorService实现获取线程返回值