ScheduledExecutorService 接口

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
警告!机器温度过高。
提醒次数限制已到,禁止任务

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值