標籤:scheduledexecutor thinking in java
package org.rui.thread.newc;import java.text.DateFormat;import java.text.SimpleDateFormat;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;/** * 溫室 控制器 * @author lenovo * */public class GreenhouseScheduler{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 thermostat){this.thermostat = thermostat;}// 發送器ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(10);/** * * @param event * @param delay 延遲 */public void scheduler(Runnable event, long delay){/** * 建並執行在給定延遲後啟用的一次性操作。 */scheduler.schedule(event, delay, TimeUnit.MILLISECONDS);}/** * 重複 * @param envent * @param initialDelay * @param period 連續執行之間的周期 時間越少 執行的越快 */public void repeat(Runnable envent, long initialDelay, long period){/** * 建立並執行一個在給定初始延遲後首次啟用的定期操作,後續操作具有給定的周期;也就是將在 initialDelay * 後開始執行,然後在 initialDelay+period 後執行,接著在 initialDelay + 2 * period 後執行,依此類推。 */scheduler.scheduleAtFixedRate(envent, initialDelay, period,TimeUnit.MILLISECONDS);}/** * inner class * 開啟 燈 */class LightOn implements Runnable{// put hardware control code here to把硬體控制碼在這裡// physically turn on the light. 身體開燈。@Overridepublic void run(){//System.out.println("Turning on lights");System.out.println("開啟電燈");light = true;}}/** * 關 * @author lenovo * */class LightOff implements Runnable{// put hardware control code here to 把硬體控制碼在這裡// physically turn off the light. 身關燈。@Overridepublic void run(){System.out.println("旋轉 關燈 ");//System.out.println("Turning off light");water = true;}}class WaterOn implements Runnable{@Overridepublic void run(){//System.out.println("Turning greenhouse water on");System.out.println("溫室水開");water = true;}}class WaterOff implements Runnable{@Overridepublic void run(){System.out.println("溫室水關");//System.out.println("Turning greenhouse water off");water = false;}}/** * 控溫器 夜晚 * @author lenovo * */class ThermostatNight implements Runnable{@Overridepublic void run(){// put hardware control code here 把硬體控制碼在這裡//System.out.println("thermostat to night setting");System.out.println("自動控溫器 夜晚設定");setThermostat("Night");}}/** * 白天 * @author lenovo * */class ThernostatDay implements Runnable{@Overridepublic void run(){// put hardware control code hereSystem.out.println("溫室白天 設定");//System.out.println("thermostat to day setting");setThermostat("Day");}}/** * 鐘 * @author lenovo * */class Bell implements Runnable{@Overridepublic void run(){System.out.println("Bing!響鈴>>");}}/** * 終止 * @author lenovo * */class Terminate implements Runnable{@Overridepublic void run(){System.out.println("Terminate》》結束");scheduler.shutdown();// must start a separate task to do this job 必須啟動一個單獨的任務來做這份工作// since the scheduler has been shut down 自調度器已經關閉new Thread(){public void run(){for (DataPoint d : data){System.out.println("DataPoint:"+d);}};}.start();}}/** * 可以持有並顯示單個的資料區段 * @author lenovo * */// inner class static class DataPoint{final Calendar time;final float temperature;final float humidity;/** * @param time * @param temperature * @param humidity */public DataPoint(Calendar time, float temperature, float humidity){this.time = time;this.temperature = temperature;this.humidity = humidity;}public String toString(){DateFormat fd=new SimpleDateFormat("yyyy/MM/dd hh:mm ss");return fd.format(time.getTime())+ String.format("temperature:%1$.1f humidity:%2$.2f",temperature, humidity);}}// //private Calendar lastTime = Calendar.getInstance();{// adjust data to the half hour 調整資料到半個小時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>());//被調度的任務,它在每次運行時,都可以產生模擬資料,並將其添加到Greenhouse的list<DataPoint>中// ineer classclass CollectData implements Runnable{@Overridepublic void run(){System.out.println("CollectData》》》run");synchronized (GreenhouseScheduler.this){// pretend the interval is longer than it is: 假裝間隔時間比是:lastTime.set(Calendar.MINUTE,lastTime.get(Calendar.MINUTE) + 30);// one in 5 chances of reversing the direction:一個在5 扭轉方向的機會:if (rand.nextInt(5) == 4){tempDirection = -tempDirection;// 方向}// store previous value: 店前一個值:lastTemp = lastTemp + tempDirection * (1.0f + rand.nextFloat());if (rand.nextInt(5) == 4){humidityDirection = -humidityDirection;}lastHumidity = lastHumidity + humidityDirection* rand.nextFloat();// calendar must be cloned , otherwise all// dataPoints hold references to the same lastTime.// for a basic object like calendar,clone() is ok.data.add(new DataPoint((Calendar) lastTime.clone(), lastTemp,lastHumidity));}}}// //////////////mainpublic static void main(String[] args){GreenhouseScheduler gh = new GreenhouseScheduler();//延遲多少時間 關閉gh.scheduler(gh.new Terminate(), 5000);// former restart class not necessary:前重啟類沒有必要:gh.repeat(gh.new Bell(), 0, 1000);//響鈴gh.repeat(gh.new ThermostatNight(), 0, 2000);//夜晚 2秒執行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 ThernostatDay(), 0, 1400);//白天gh.repeat(gh.new CollectData(), 500, 500);}}/*** * output: * Bing!響鈴>>自動控溫器 夜晚設定開啟電燈旋轉 關燈 溫室水開溫室水關溫室白天 設定開啟電燈開啟電燈旋轉 關燈 CollectData》》》run溫室水開開啟電燈開啟電燈旋轉 關燈 溫室水關Bing!響鈴>>開啟電燈CollectData》》》run開啟電燈溫室水開旋轉 關燈 開啟電燈溫室白天 設定CollectData》》》run開啟電燈溫室水關旋轉 關燈 開啟電燈溫室水開Bing!響鈴>>CollectData》》》run旋轉 關燈 開啟電燈自動控溫器 夜晚設定開啟電燈開啟電燈旋轉 關燈 溫室水開溫室水關CollectData》》》run開啟電燈開啟電燈旋轉 關燈 溫室白天 設定開啟電燈CollectData》》》run溫室水開Bing!響鈴>>旋轉 關燈 溫室水關開啟電燈開啟電燈CollectData》》》run旋轉 關燈 溫室水開開啟電燈開啟電燈Bing!響鈴>>自動控溫器 夜晚設定旋轉 關燈 溫室水關CollectData》》》run開啟電燈開啟電燈溫室水開溫室白天 設定開啟電燈旋轉 關燈 CollectData》》》run開啟電燈開啟電燈溫室水關溫室水開旋轉 關燈 Bing!響鈴>>開啟電燈CollectData》》》runTerminate》》結束DataPoint:2015/07/19 09:00 00temperature:66.4 humidity:50.05DataPoint:2015/07/19 09:30 00temperature:68.0 humidity:50.47DataPoint:2015/07/19 10:00 00temperature:69.7 humidity:51.42DataPoint:2015/07/19 10:30 00temperature:70.8 humidity:50.87DataPoint:2015/07/19 11:00 00temperature:72.0 humidity:50.32DataPoint:2015/07/19 11:30 00temperature:73.2 humidity:49.92DataPoint:2015/07/20 12:00 00temperature:71.9 humidity:49.81DataPoint:2015/07/20 12:30 00temperature:70.1 humidity:50.25DataPoint:2015/07/20 01:00 00temperature:68.9 humidity:51.00DataPoint:2015/07/20 01:30 00temperature:67.7 humidity:50.21 */
java並發 使用ScheduledExecutor的溫室控制器--thinking in java 21.7.5