Thinking in Java学习笔记ScheduledThreadPoolExecutor

本文介绍了一个基于ScheduledThreadPoolExecutor的温室自动化控制系统实现。系统定时执行开关灯、调节温控、收集环境数据等功能,通过不同任务间的协调运行,实现了温室环境的智能化管理。

温室的开灯、关灯、开水、关水、白天热电、夜晚热电、收集数据(时间温度湿度)

ScheduledThreadPoolExecutor的schedule(event, delay, TimeUnit.MILLISECONDS)方法,在delay参数指定的时间后,只执行一次


ScheduledThreadPoolExecutor的.scheduleAtFixedRate(event, initialDelay, period, TimeUnit.MILLISECONDS)方法,在初始时间执行一次,且按照period指定的时间间隔周期的执行,相同时间的任务按照FIFO的顺序被调度


package com.test.concurrent;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class GreenHouseScheduler {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		GreenHouseScheduler gh=new GreenHouseScheduler();
		gh.schedule(gh.new Terminate(),5000);
		gh.repeat(gh.new Bell(), 0, 1000);
		gh.repeat(gh.new ThermostatNight(),0,2000);
		gh.repeat(gh.new LightOn(), 0, 200);
		gh.repeat(gh.new LightOff(),0,400);
		gh.repeat(gh.new WaterOn(), 0, 600);
		gh.repeat(gh.new WaterOff(),0,800);
		gh.repeat(gh.new ThermostatDay(),0,1400);
		gh.repeat(gh.new CollectData(), 500, 500);
	}
	private volatile boolean light=false;
	private volatile boolean water=false;
	private String thermostat="Day";
	public synchronized String getThermostat(){
		return thermostat;
	}
	public synchronized void setThermostat(String value){
		thermostat=value;
	}
	<span style="color:#ff0000;">ScheduledThreadPoolExecutor scheduler=new ScheduledThreadPoolExecutor(10);
	public void schedule(Runnable event, long delay){
		scheduler.schedule(event, delay, TimeUnit.MILLISECONDS);
	}
	public void repeat(Runnable event,long initialDelay,long period){
		scheduler.scheduleAtFixedRate(event, initialDelay, period, TimeUnit.MILLISECONDS);
	}</span>
	class LightOn implements Runnable{
		@Override
		public void run(){
			//put hardware control code here to physically turn on the ligth
			System.out.println("Turn on the lights!");
			light=true;
		}
	}
	class LightOff implements Runnable{
		@Override 
		public void run(){
			System.out.println("Turn lights off!!");
			light=false;
		}
	}
	class WaterOn implements Runnable{
		@Override
		public void run(){
			System.out.println("Turning GreenHouse water on");
			water=true;
		}
	}
	class WaterOff implements Runnable{
		@Override
		public void run(){
			System.out.println("Turning GreenHouse water off");
			water=false;
		}
	}
	class ThermostatNight implements Runnable{
		@Override
		public void run(){
			System.out.println("Set thermostat night!!");
			setThermostat("Night");
		}
	}
	class ThermostatDay implements Runnable{
		@Override
		public void run(){
			System.out.println("Set thermostat day!!");
			setThermostat("Day");
		}
	}
	class Bell implements Runnable{
		@Override
		public void run(){
			System.out.println("--------belling------------");
		}
	}
	class Terminate implements Runnable{
		@Override
		public void run(){
			System.out.println("***Terminating****");
			scheduler.shutdownNow();
			new Thread(){
				public void run(){
					for(DataPoint d:data){
						System.out.println(d);
					}
				}
			}.start();
		}
	}
	static class DataPoint{
		final Calendar time;
		final float temperature;
		final float humidity;
		public DataPoint(Calendar d,float temp, float hum){
			time=d;
			temperature=temp;
			humidity=hum;
		}
		public String toString(){
			return time.getTime()+" temperature:"+temperature+"    humidity:"+humidity;
		}
	}
	private Calendar lastTime=Calendar.getInstance();
	{
		lastTime.set(Calendar.MINUTE,30);
		lastTime.set(Calendar.SECOND, 00);
	}
	private float lastTemp=65.0f;
	private int tempDirection=+1;
	private float lastHumidity=50.0f;
	private int humidityDirection=+1;
	private Random rand=new Random(47);
	List<DataPoint> data=Collections.synchronizedList(new ArrayList<DataPoint>());
	
	class CollectData implements Runnable{
		@Override
		public void run(){
			System.out.println("Collecting Data::::::::");
			synchronized(GreenHouseScheduler.this){
				lastTime.set(Calendar.MINUTE, lastTime.get(Calendar.MINUTE)+30);
				if(rand.nextInt(5)==4)
					tempDirection=-tempDirection;
					lastTemp=lastTemp+tempDirection*(rand.nextFloat()+1.0f);
					
					if(rand.nextInt(5)==4)
						humidityDirection=-humidityDirection;
					lastHumidity=lastHumidity+humidityDirection*rand.nextFloat();
					
					data.add(new DataPoint((Calendar)lastTime.clone(),lastTemp,lastHumidity));
			}
		}
	}
}

执行结果如下:


--------belling------------ //第0秒开始,按照顺序首次执行了初始执行时间为0的所有任务
Set thermostat night!!
Turn on the lights!
Turn lights off!!
Turning GreenHouse water on
Turning GreenHouse water off
Set thermostat day!!
Turn on the lights!
Turn on the lights!
Turn lights off!!
Collecting Data::::::::
Turn on the lights!
Turning GreenHouse water on
Turn on the lights!
Turn lights off!!
Turning GreenHouse water off
--------belling------------
Turn on the lights!
Collecting Data::::::::
Turn on the lights!
Turn lights off!!
Turning GreenHouse water on
Turn on the lights!
Set thermostat day!!
Collecting Data::::::::
Turn on the lights!
Turn lights off!!
Turning GreenHouse water off
Turn on the lights!
Turning GreenHouse water on
--------belling------------
Set thermostat night!!
Turn on the lights!
Turn lights off!!
Collecting Data::::::::
Turn on the lights!
Turn on the lights!
Turn lights off!!
Turning GreenHouse water on
Turning GreenHouse water off
Collecting Data::::::::
Turn on the lights!
Turn on the lights!
Turn lights off!!
Set thermostat day!!
--------belling------------
Turn on the lights!
Turning GreenHouse water on
Collecting Data::::::::
Turn on the lights!
Turn lights off!!
Turning GreenHouse water off
Turn on the lights!
Collecting Data::::::::
Turn on the lights!
Turn lights off!!
Turning GreenHouse water on
Turn on the lights!
--------belling------------
Set thermostat night!!
Turn on the lights!
Turn lights off!!
Turning GreenHouse water off
Collecting Data::::::::
Turn on the lights!
Turning GreenHouse water on
Set thermostat day!!
Turn on the lights!
Turn lights off!!
Collecting Data::::::::
Turn on the lights!
Turn on the lights!
Turn lights off!!
Turning GreenHouse water on
Turning GreenHouse water off
***Terminating****
Thu Feb 26 17:00:00 CST 2015 temperature:66.399826    humidity:50.053413
Thu Feb 26 17:30:00 CST 2015 temperature:67.97782    humidity:50.47043
Thu Feb 26 18:00:00 CST 2015 temperature:69.71517    humidity:51.421486
Thu Feb 26 18:30:00 CST 2015 temperature:70.82953    humidity:50.874798
Thu Feb 26 19:00:00 CST 2015 temperature:72.03096    humidity:50.321068
Thu Feb 26 19:30:00 CST 2015 temperature:73.18805    humidity:49.92445
Thu Feb 26 20:00:00 CST 2015 temperature:71.93828    humidity:49.810783
Thu Feb 26 20:30:00 CST 2015 temperature:70.13756    humidity:50.251114
Thu Feb 26 21:00:00 CST 2015 temperature:68.93982    humidity:50.995125

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值