1 Star 4 Fork 0

sumAll/Java编程思想(第四版)

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
GreenhouseScheduler.java 4.77 KB
一键复制 编辑 原始数据 按行查看 历史
sumAll 提交于 4年前 . 书本P730的温度控制器
package net.mindView.concurrency;
import java.util.*;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
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 value){
thermostat = value;
}
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);
}
//事件
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("Turning 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");
setThermostat("Day");
}
}
class Bell implements Runnable{
public void run(){
System.out.println("Bing!");
}
}
class Terminate implements Runnable{
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;
}
@Override
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>());
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,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);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sumall/Thinking-in-Java.git
git@gitee.com:sumall/Thinking-in-Java.git
sumall
Thinking-in-Java
Java编程思想(第四版)
master

搜索帮助