java延迟任务调度_Java的ScheduledExecutorService任务调度使用(延时,定时执行)

packageuu;importjava.util.concurrent.Executors;importjava.util.concurrent.Future;importjava.util.concurrent.ScheduledExecutorService;importjava.util.concurrent.ScheduledFuture;importjava.util.concurrent.ThreadFactory;importjava.util.concurrent.TimeUnit;importjava.util.concurrent.atomic.AtomicInteger;/*** 任务调度工具类

*

*@authorlinxu

* @date 2020/07/18

**/

public classExecutorUtil {public static final ScheduledExecutorService COMMON_LOGIC_EXECUTOR = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors() * 2, newThreadFactory() {

AtomicInteger count= new AtomicInteger(0);

@OverridepublicThread newThread(Runnable r) {int curCount =count.incrementAndGet();

System.err.println("任务调度线程池"+curCount);return new Thread(r, "任务调度线程池" +curCount);

}

});public static ScheduledFuture> scheduleAtFixedRate(Runnable command, long initialDelay, longperiod) {returnCOMMON_LOGIC_EXECUTOR.scheduleAtFixedRate(command, initialDelay, period, TimeUnit.MILLISECONDS);

}public static ScheduledFuture> scheduleAtFixedRate(Runnable command, long initialDelay, longperiod,

TimeUnit unit) {returnCOMMON_LOGIC_EXECUTOR.scheduleAtFixedRate(command, initialDelay, period, unit);

}public static ScheduledFuture> scheduleWithFixedDelay(Runnable command, long initialDelay, longperiod,

TimeUnit unit) {returnCOMMON_LOGIC_EXECUTOR.scheduleWithFixedDelay(command, initialDelay, period, unit);

}//延时执行,不重复

public static ScheduledFuture> schedule(Runnable command, longdelay, TimeUnit unit) {returnCOMMON_LOGIC_EXECUTOR.schedule(command, delay, unit);

}public static Future>submit(Runnable task) {returnCOMMON_LOGIC_EXECUTOR.submit(task);

}//优雅的关闭线程池

public static voidclose() {

COMMON_LOGIC_EXECUTOR.shutdown();try{// //等待未完成任务结束

System.err.println("等待未完成任务结束");if (!COMMON_LOGIC_EXECUTOR.awaitTermination(60, TimeUnit.SECONDS)) {

COMMON_LOGIC_EXECUTOR.shutdown();

}// //等待任务取消的响应

System.err.println("等待任务取消的响应");if (!COMMON_LOGIC_EXECUTOR.awaitTermination(60, TimeUnit.SECONDS)) {

}

}catch(Exception e) {

COMMON_LOGIC_EXECUTOR.shutdown();//保留中断状态

System.err.println("保留中断状态");

Thread.currentThread().interrupt();

}

}public static void main(String[] args) throwsException {

System.err.println("jvm空闲内存:"+Runtime.getRuntime().freeMemory());

System.err.println("jvm最大内存:"+Runtime.getRuntime().maxMemory());

System.err.println("jvm总内存:"+Runtime.getRuntime().totalMemory());final ScheduledFuture> scheduledFuture = schedule(newRunnable() {

@Overridepublic voidrun() {//TODO Auto-generated method stub

System.err.println("sdmnclksdklds1");

}

},3000, TimeUnit.MILLISECONDS);

schedule(newRunnable() {

@Overridepublic voidrun() {//TODO Auto-generated method stub

System.err.println("sdmnclksdklds2");

}

},1000, TimeUnit.MILLISECONDS);newThread() {public voidrun() {try{

Thread.sleep(1000);

System.err.println("是否完成:"+scheduledFuture.isDone());

}catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}.start();newThread() {public voidrun() {try{

Thread.sleep(5000);

System.err.println("是否完成:"+scheduledFuture.isDone());

close();

}catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}.start();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>