Thinking in Java學習筆記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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章