java并发 使用ScheduledExecutor的温室控制器–thinking in java 21.7.5

java并发 使用ScheduledExecutor的温室控制器–thinking in java 21.7.5

大家好,又见面了,我是全栈君。

java并发 使用ScheduledExecutor的温室控制器--thinking in java 21.7.5

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. 身体开灯。

@Override public 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. 身关灯。 @Override public void run() { System.out.println("旋转 关灯 "); // System.out.println("Turning off light"); water = true; } } class WaterOn implements Runnable { @Override public void run() { //System.out.println("Turning greenhouse water on"); System.out.println("温室水开"); water = true; } } class WaterOff implements Runnable { @Override public void run() { System.out.println("温室水关"); //System.out.println("Turning greenhouse water off"); water = false; } } /** * 控温器 夜晚 * @author lenovo * */ class ThermostatNight implements Runnable { @Override public 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 { @Override public void run() { // put hardware control code here System.out.println("温室白天 设置"); // System.out.println("thermostat to day setting"); setThermostat("Day"); } } /** * 钟 * @author lenovo * */ class Bell implements Runnable { @Override public void run() { System.out.println("Bing!响铃>>"); } } /** * 终止 * @author lenovo * */ class Terminate implements Runnable { @Override public 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 class class CollectData implements Runnable { @Override public 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)); } } } // //////////////main public 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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115507.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • JAVA Calendar详解

    JAVA Calendar详解(在文章的最后,将会介绍Date类,如果有兴趣,可以直接翻到最后去阅读)究竟什么是一个Calendar呢?中文的翻译就是日历,那我们立刻可以想到我们生活中有阳(公)历、阴(农)历之分。它们的区别在哪呢?比如有:月份的定义-阳`(公)历一年12个月,每个月的天数各不同;阴(农)历,每个月固定28天每周的第一天-阳(公)历星期日是第一天;阴(农)历,星期一是第一天实际上,在历…

    2022年6月12日
    28
  • 开启Scrapy爬虫之路

    开启Scrapy爬虫之路七夜大佬的《python爬虫开发与项目实战》,买了好多年了,学习了好多东西,基本上爬虫都是在这里面学的,后期的scrapy框架爬虫一直不得门而入,前段时间补了下面向对象的知识,今天突然顿悟了!写个笔记记录下学习过程

    2022年6月26日
    23
  • shiro面试题「建议收藏」

    shiro面试题「建议收藏」1、什么是ShiroApacheShiro是Java 的一个安全(权限)框架。Shiro可以非常容易的开发出足够好的应用,其不仅可以用在JavaSE环境,也可以用在JavaEE环境。Shiro可以完成:认证、授权、加密、会话管理、与Web集成、缓存等。2、描述Shiro认证流程1、收集用户身份/凭证2、调用Subject.login进行登录3、创建自定义的R…

    2022年10月15日
    2
  • 安卓手机修改ntp服务器,修改安卓手机ntp服务器地址「建议收藏」

    安卓手机修改ntp服务器,修改安卓手机ntp服务器地址「建议收藏」修改安卓手机ntp服务器地址内容精选换一换TTL(Time-To-Live)指解析记录在本地DNS服务器中的缓存时间。本地DNS服务器指用户客户端(手机、电脑等)连接Internet网络使用的DNS,默认使用的DNS是宽带运营商自动分配的DNS服务器,用户也可以将该DNS修改为公共DNS服务器,例如,114.114.114.114、8.8.8.8。通过华为云购买的弹性云服务器默认硬件要求如表1所…

    2022年6月9日
    252
  • MCP2515模块_mcp2515接收错误

    MCP2515模块_mcp2515接收错误1、在配置Linux编译选项时,开启相应的SPI选项,如下所示->DeviceDrivers->SPIsupportSPIsupport***SPIMasterControllerDrivers***-*-BitbangingSPImasterSamsungS3C24XXseriesSPI<>SamsungS3C24XXserie…

    2025年6月19日
    3
  • 死链接检测 java,【死链接检测】工具查询方法及死链接处理方法

    死链接检测 java,【死链接检测】工具查询方法及死链接处理方法【死链接检测】工具查询方法及死链接处理方法死链接不但影响用户的体验,而且影响网站的跳出率,网站的跳出率直接关系到网站的排名。网站死链接量达到一定的程度,甚至网站会降权或者被K站,站长们应改高度的重视。死链接404页面1.网站死连接的查找。在360浏览器里——找到扩展——查找输入死链接,安装好插件。安装好以后,浏览器的上面就有一个这样的图标。打开你的网站,点击网页链接检查。出现下面的图片。然后收集死…

    2022年7月22日
    10

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号