(原创)定时线程池中scheduleWithFixedDelay和scheduleAtFi。。。

更新时间:2023-07-08 08:47:22 阅读: 评论:0

(原创)定时线程池中scheduleWithFixedDelay和
scheduleAtFi。。。
scheduleAtFixedRate
没有什么歧义,很容易理解,就是每隔多少时间,固定执⾏任务。
scheduleWithFixedDelay ⽐较容易有歧义
貌似也是推迟⼀段时间执⾏任务,但Oracle的解释如下,delay的意思是当结束前⼀个执⾏后延迟的时间
scheduleWithFixedDelay Parameters:
command - the task to execute
initialDelay - the time to delay first execution
delay - the delay between the termination of one execution and the commencement of the next
unit - the time unit of the initialDelay and delay parameters
小学中高年级
scheduleWithFixedDelay ⽐如当前⼀个任务结束的时刻,开始结算间隔时间,如0秒开始执⾏第⼀次任务,任务耗时5秒,任务间隔时间3秒,那么第⼆次任务执⾏的时间是在第8秒开始。酸碱中和
import java.util.Date;
public class WorkerThread implements Runnable{
private String command;
public WorkerThread(String s){
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+" Start. Time = "+new Date());
processCommand();
System.out.println(Thread.currentThread().getName()+" End. Time = "+new Date());
}
浙大科技园private void processCommand() {葱香肉饼
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public String toString(){
and;
}
}
import java.util.Date;
import urrent.Executors;
import urrent.ScheduledExecutorService;
import urrent.TimeUnit;
public class ScheduledThreadPool {
public static void main(String[] args) throws InterruptedException {
ScheduledExecutorService scheduledThreadPool = wScheduledThreadPool(5);
System.out.println("Current Time = "+new Date());
for(int i=0; i<10; i++){
Thread.sleep(1000);
棒球帽品牌十大排行榜解决拼音WorkerThread worker = new WorkerThread("do heavy processing");
绣眼鸟//scheduledThreadPool.schedule(worker, 3, TimeUnit.SECONDS);
//scheduledThreadPool.scheduleAtFixedRate(worker, 5, 5, TimeUnit.SECONDS);            scheduledThreadPool.scheduleWithFixedDelay(worker, 5, 3, TimeUnit.SECONDS);        }
Thread.sleep(300000);
scheduledThreadPool.shutdown();
while(!scheduledThreadPool.isTerminated()){
}
System.out.println("Finished all threads");
}
什么今什么昔成语}

本文发布于:2023-07-08 08:47:22,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1072743.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:任务   排行榜   结束   时刻   时间   歧义
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图