package com.xyw.concurrent.blog;
import java.util.concurrent.*;
import java.util.*;
/*
* 通过模拟温室之中的温度变化的情况,进行仿真的操作,体现出Java 并发的知识
*/
public class GreenhouseScheduler {
private volatile boolean light = false;
private volatile boolean water = false;
private String thermostat = "Day"; // thermostat 有恒温器的意思
public synchronized String getThermostat(){
return thermostat;
}
public synchronized void setThermostat(String value){
thermostat = value;
}
ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(10); // 预定义工具
public void schedule(Runnable event, long delay){
scheduler.schedule(event, delay, TimeUnit.MILLISECONDS);
} // 调用schedule 来进行设置将来某个时候执行的工作
public void repeat(Runnable event, long initialDelay,long period){
scheduler.scheduleAtFixedRate(event, initialDelay, period, TimeUnit.MILLISECONDS);
} // 调用 scheduleAtFixedRate 来进行每隔规则的时间,重复指向的任务
class LightOn implements Runnable{
public void run(){
System.out.println("Turning on lights");
light = true;
}
} // 开灯任务
class LightOff implements Runnable{
public void run(){
System.out.println("Turning off lights");
light = false;
}
} // 关灯任务
class WaterOn implements Runnable{
public void run(){
System.out.println("Turning greenhouse water on");
water = true;
}
} // 放水任务
class WaterOff implements Runnable{
public void run(){
System.out.println("Turing greenHouse water off");
water = false;
}
} // 关水龙头的任务
class ThermostatNight implements Runnable{
public void run(){
System.out.println("Thermostat to night setting");
setThermostat("Night");
}
}// 晚间恒温器状态
class ThermostatDay implements Runnable{
public void run(){
System.out.println("Thermostat to day setting");
}
}// 白天的恒温器状态
class Bell implements Runnable{
public void run(){
System.out.println("Bing!");
}
}// 铃响
class Terminate implements Runnable{
public void run(){
System.out.println("Termination");
scheduler.shutdown();
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() + String.format("temperature: %1$.1f humidity: %2$.2f", temperature, 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>());
// 在这里持有DataPoint 的 List 的所有方法都是 synchronized 的,这是因为 在 List 被创建时,使用了 java.util.Collection
// 使用工具, syncchronizedList();
class CollectData implements Runnable{
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*(1.0f + rand.nextFloat());
if(rand.nextInt(5) == 4){
humidityDirection = - humidityDirection;
}
lastHumidity = lastHumidity + humidityDirection * rand.nextFloat();
data.add(new DataPoint((Calendar)lastTime.clone(), lastTemp, lastHumidity));
}
}
}
public static void main(String[] args){
GreenhouseScheduler gh = new GreenhouseScheduler();
gh.schedule(gh.new Terminate(), 5000);
gh.repeat(gh.new Bell(), 0, 100);
gh.repeat(gh.new ThermostatNight(), 0, 200);
gh.repeat(gh.new LightOn(), 0, 200);
gh.repeat(gh.new LightOff(), 0, 200);
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);// 搜集信息
//其中的仿真的操作不够晚上,关灯,和开灯以及
// 开关水龙头对温度和湿度的影响都是没有进行体现的,这里值得完善一下。
// 但是通过这个例子来展示 ScheduledThreadPoolExecutor 还是可以体现的
}
}
温室环境仿真的运行结果:
Bing!
Thermostat to night setting
Turning on lights
Turning off lights
Turning greenhouse water on
Turing greenHouse water off
Thermostat to day setting
Bing!
Bing!
Thermostat to night setting
Turning on lights
Turning off lights
Bing!
Bing!
Turning off lights
Turning on lights
Thermostat to night setting
Bing!
Collecting Data
Bing!
Thermostat to night setting
Turning on lights
Turning off lights
Turning greenhouse water on
Bing!
Bing!
Thermostat to night setting
Turning on lights
Turning off lights
Turing greenHouse water off
Bing!
Bing!
Turning on lights
Turning off lights
Thermostat to night setting
Collecting Data
Bing!
Bing!
Thermostat to night setting
Turning on lights
Turning off lights
Turning greenhouse water on
Bing!
Bing!
Thermostat to night setting
Turning off lights
Turning on lights
Thermostat to day setting
Bing!
Collecting Data
Bing!
Turning off lights
Turning on lights
Thermostat to night setting
Turing greenHouse water off
Bing!
Bing!
Thermostat to night setting
Turning on lights
Turning off lights
Turning greenhouse water on
Bing!
Bing!
Thermostat to night setting
Turning off lights
Turning on lights
Collecting Data
Bing!
Bing!
Turning on lights
Thermostat to night setting
Turning off lights
Bing!
Bing!
Thermostat to night setting
Turning on lights
Turning off lights
Turning greenhouse water on
Turing greenHouse water off
Bing!
Collecting Data
Bing!
Turning on lights
Thermostat to night setting
Turning off lights
Bing!
Bing!
Turning on lights
Thermostat to night setting
Turning off lights
Thermostat to day setting
Bing!
Bing!
Thermostat to night setting
Turning on lights
Turning off lights
Turning greenhouse water on
Collecting Data
Bing!
Bing!
Turning on lights
Thermostat to night setting
Turning off lights
Turing greenHouse water off
Bing!
Bing!
Thermostat to night setting
Turning on lights
Turning off lights
Bing!
Collecting Data
Bing!
Turning on lights
Thermostat to night setting
Turning off lights
Turning greenhouse water on
Bing!
Bing!
Turning on lights
Thermostat to night setting
Turning off lights
Bing!
Bing!
Turning on lights
Thermostat to night setting
Turning off lights
Turing greenHouse water off
Collecting Data
Bing!
Bing!
Thermostat to night setting
Turning on lights
Turning off lights
Turning greenhouse water on
Thermostat to day setting
Bing!
Bing!
Turning on lights
Thermostat to night setting
Turning off lights
Bing!
Collecting Data
Bing!
Turning off lights
Turning on lights
Thermostat to night setting
Bing!
Bing!
Thermostat to night setting
Turning on lights
Turning off lights
Turning greenhouse water on
Turing greenHouse water off
Bing!
Bing!
Termination
Fri Feb 26 22:00:00 CST 2016temperature: 66.4 humidity: 50.05
Fri Feb 26 22:30:00 CST 2016temperature: 68.0 humidity: 50.47
Fri Feb 26 23:00:00 CST 2016temperature: 69.7 humidity: 51.42
Fri Feb 26 23:30:00 CST 2016temperature: 70.8 humidity: 50.87
Sat Feb 27 00:00:00 CST 2016temperature: 72.0 humidity: 50.32
Sat Feb 27 00:30:00 CST 2016temperature: 73.2 humidity: 49.92
Sat Feb 27 01:00:00 CST 2016temperature: 71.9 humidity: 49.81
Sat Feb 27 01:30:00 CST 2016temperature: 70.1 humidity: 50.25
Sat Feb 27 02:00:00 CST 2016temperature: 68.9 humidity: 51.00