前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ScheduledExecutorService 接口[通俗易懂]

ScheduledExecutorService 接口[通俗易懂]

作者头像
全栈程序员站长
发布2022-08-22 13:00:45
7290
发布2022-08-22 13:00:45
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

newScheduledThreadPool() 或者newSingleThreadScheduled-Executor()方法:延迟执行、周期性执行的执行器 如果想在某一段时间之后执行线程操作,或者周期性地重复执行线程操作,则可以使用工厂类Executors的newScheduledThreadPool()方法或者 newSingleThreadScheduled-Executor()方法。 newScheduledThreadPool()方法使用给定数目的线程来调度执行任务,而newSingleThreadScheduledExecutor()方法在一个单独的线程中调度任务。 这两个方法都将返回一个ScheduledExecutorService线程池对象。 ScheduledExecutorService接口 ScheduledExecutorService接口从ExecutorService接口继承而来,可用于在给定的延迟后运行的某个任务,或者周期性的执行某个任务。 schedule()方法用于创建并执行给定的延迟的任务,返回的ScheduledFuture对象可以取消执行,或检查执行状态。scheduleAtFixedRate 和scheduleWithFixedDelay用于创建并执行一个周期性或者 固定延迟任务,直到任务取消。 在schedule()方法中,延迟时间一般大于0,但也允许取值为0或者负数(非周期性执行),在这种情况下,认为是立刻执行。 TimeUnit 用于指明时间单位,时间都是相对的时间,而不是绝对的时间。例如,在某一个日期之后运行,则可以使用下面的语句。 scheduled(commad,date.getTime() -System.currentTimeMills,TimeUnit.MILLISECONDS) ScheduledFuture接口 ScheduledExecutorService接口的4个方法都将返回ScheduledFuture对象,ScheduledFuture也是一个接口,他从Delay和Future接口继承而来,表示一个延迟的、结果可接受的操作。 该接口的getDelay方法用于获得延迟时间,get()方法用于获得操作结果,cancel()方法用于取消一个任务。

demo 示例: 监控一个设备的工作温度,当温度超过10°C后,每隔1s发出一次警告,如果连续发出10报警后,仍没有处理,则停止设备运行。

分析 :设置两个线程,一个线程表示设备运行。一个线程监视设备运行,采用ScheduleAtFixedRate()方法来调度,当设备警告10次,采用取消cancel()或者shutdown()方法关闭设备。 //设备线程类 public class Machine implements Runnable{ int temperature; Machine(int temperature){ this.temperature = temperature; } public void run(){ perform(); temperature++; System.out.println(“机器的工作温度在升高,当前的温度:”+temperature); } private void perform(){ int temp = (int)(Math.random()*Integer.MAX_VALUE); int sum = 0; for(int i=0;i<temp;i++){ sum += i; } } private int getTemperature(){ return temperature; } } //监控设备线程类 public class Monitor implements Runnable{ Machine machine; ScheduledExecutorService scheduler; static int n = 0; Monitor(Machine machine,ScheduledExecutorService scheduler){ this.machine = machine; this.scheduler = scheduler; } public void run(){ if(machine.temperature>=10){ System.out.println(“警告!机器温度过高。”); n++; } if(n>10){ System.out.println(“提醒次数限制已到,禁止任务”); scheduler.shutdown(); } } } //测试启动类 public class Index{ public static void main(String[] args){ ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2); Machine machine = new Machine(0); Monitor monitor = new Monitor(machine,scheduler); scheduler.scheduleAtFixedRate(machine,1,2,TimeUnit.SECONDS); scheduler.scheduleAtFixedRate(monitor,0,1,TimeUnit.SECONDS); } }

运行结果:

机器的工作温度在升高,当前的温度:1 机器的工作温度在升高,当前的温度:2 机器的工作温度在升高,当前的温度:3 机器的工作温度在升高,当前的温度:4 机器的工作温度在升高,当前的温度:5 机器的工作温度在升高,当前的温度:6 机器的工作温度在升高,当前的温度:7 机器的工作温度在升高,当前的温度:8 机器的工作温度在升高,当前的温度:9 机器的工作温度在升高,当前的温度:10 警告!机器温度过高。 警告!机器温度过高。 机器的工作温度在升高,当前的温度:11 警告!机器温度过高。 警告!机器温度过高。 机器的工作温度在升高,当前的温度:12 警告!机器温度过高。 警告!机器温度过高。 机器的工作温度在升高,当前的温度:13 警告!机器温度过高。 警告!机器温度过高。 机器的工作温度在升高,当前的温度:14 警告!机器温度过高。 警告!机器温度过高。 机器的工作温度在升高,当前的温度:15 警告!机器温度过高。 提醒次数限制已到,禁止任务

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/137075.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年5月5,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档