1. package org.rui.thread.newc;
  2.  
  3. import java.text.DateFormat;
  4. import java.text.SimpleDateFormat;
  5. import java.util.ArrayList;
  6. import java.util.Calendar;
  7. import java.util.Collections;
  8. import java.util.List;
  9. import java.util.Random;
  10. import java.util.concurrent.ScheduledThreadPoolExecutor;
  11. import java.util.concurrent.TimeUnit;
  12.  
  13. /**
  14. * 温室 控制器
  15. * @author lenovo
  16. *
  17. */
  18. public class GreenhouseScheduler
  19. {
  20. private volatile boolean light = false;// 光
  21. private volatile boolean water = false;// 水
  22. private String thermostat = "Day";// 自己主动调温器
  23.  
  24. public synchronized String getThermostat()
  25. {
  26. return thermostat;
  27. }
  28.  
  29. public synchronized void setThermostat(String thermostat)
  30. {
  31. this.thermostat = thermostat;
  32. }
  33.  
  34. // 调度程序
  35. ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(10);
  36.  
  37. /**
  38. *
  39. * @param event
  40. * @param delay 延迟
  41. */
  42. public void scheduler(Runnable event, long delay)
  43. {
  44.  
  45. /**
  46. * 建并运行在给定延迟后启用的一次性操作。
  47. */
  48. scheduler.schedule(event, delay, TimeUnit.MILLISECONDS);
  49. }
  50.  
  51. /**
  52. * 反复
  53. * @param envent
  54. * @param initialDelay
  55. * @param period 连续运行之间的周期 时间越少 运行的越快
  56. */
  57. public void repeat(Runnable envent, long initialDelay, long period)
  58. {
  59.  
  60. /**
  61. * 创建并运行一个在给定初始延迟后首次启用的定期操作。兴许操作具有给定的周期。也就是将在 initialDelay
  62. * 后開始运行。然后在 initialDelay+period 后运行。接着在 initialDelay + 2 * period 后运行,依此类推。
  63. */
  64. scheduler.scheduleAtFixedRate(envent, initialDelay, period,
  65. TimeUnit.MILLISECONDS);
  66. }
  67.  
  68. /**
  69. * inner class
  70. * 打开 灯
  71. */
  72. class LightOn implements Runnable
  73. {
  74.  
  75. // put hardware control code here to把硬件控制代码在这里
  76. // physically turn on the light. 身体开灯。
  77.  
  78. @Override
  79. public void run()
  80. {
  81. //System.out.println("Turning on lights");
  82. System.out.println("打开电灯");
  83.  
  84. light = true;
  85.  
  86. }
  87.  
  88. }
  89.  
  90. /**
  91. * 关
  92. * @author lenovo
  93. *
  94. */
  95. class LightOff implements Runnable
  96. {
  97.  
  98. // put hardware control code here to 把硬件控制代码在这里
  99. // physically turn off the light. 身关灯。
  100. @Override
  101. public void run()
  102. {
  103. System.out.println("旋转 关灯 ");
  104. // System.out.println("Turning off light");
  105.  
  106. water = true;
  107. }
  108. }
  109.  
  110. class WaterOn implements Runnable
  111. {
  112.  
  113. @Override
  114. public void run()
  115. {
  116. //System.out.println("Turning greenhouse water on");
  117. System.out.println("温室水开");
  118.  
  119. water = true;
  120.  
  121. }
  122.  
  123. }
  124.  
  125. class WaterOff implements Runnable
  126. {
  127.  
  128. @Override
  129. public void run()
  130. {
  131. System.out.println("温室水关");
  132.  
  133. //System.out.println("Turning greenhouse water off");
  134. water = false;
  135.  
  136. }
  137.  
  138. }
  139.  
  140. /**
  141. * 控温器 夜晚
  142. * @author lenovo
  143. *
  144. */
  145. class ThermostatNight implements Runnable
  146. {
  147.  
  148. @Override
  149. public void run()
  150. {
  151. // put hardware control code here 把硬件控制代码在这里
  152. //System.out.println("thermostat to night setting");
  153. System.out.println("自己主动控温器 夜晚设置");
  154. setThermostat("Night");
  155.  
  156. }
  157. }
  158.  
  159. /**
  160. * 白天
  161. * @author lenovo
  162. *
  163. */
  164. class ThernostatDay implements Runnable
  165. {
  166.  
  167. @Override
  168. public void run()
  169. {
  170. // put hardware control code here
  171. System.out.println("温室白天 设置");
  172. // System.out.println("thermostat to day setting");
  173.  
  174. setThermostat("Day");
  175.  
  176. }
  177. }
  178.  
  179. /**
  180. * 钟
  181. * @author lenovo
  182. *
  183. */
  184. class Bell implements Runnable
  185. {
  186.  
  187. @Override
  188. public void run()
  189. {
  190. System.out.println("Bing!响铃>>");
  191. }
  192.  
  193. }
  194.  
  195. /**
  196. * 终止
  197. * @author lenovo
  198. *
  199. */
  200. class Terminate implements Runnable
  201. {
  202.  
  203. @Override
  204. public void run()
  205. {
  206. System.out.println("Terminate》》结束");
  207. scheduler.shutdown();
  208. // must start a separate task to do this job 必须启动一个单独的任务来做这份工作
  209. // since the scheduler has been shut down 自调度器已经关闭
  210. new Thread()
  211. {
  212. public void run()
  213. {
  214. for (DataPoint d : data)
  215. {
  216. System.out.println("DataPoint:"+d);
  217. }
  218. };
  219. }.start();
  220.  
  221. }
  222. }
  223.  
  224. /**
  225. * 能够持有并显示单个的数据段
  226. * @author lenovo
  227. *
  228. */
  229. // inner class
  230. static class DataPoint
  231. {
  232. final Calendar time;
  233. final float temperature;
  234. final float humidity;
  235.  
  236. /**
  237. * @param time
  238. * @param temperature
  239. * @param humidity
  240. */
  241. public DataPoint(Calendar time, float temperature, float humidity)
  242. {
  243.  
  244. this.time = time;
  245. this.temperature = temperature;
  246. this.humidity = humidity;
  247. }
  248.  
  249. public String toString()
  250. {
  251. DateFormat fd=new SimpleDateFormat("yyyy/MM/dd hh:mm ss");
  252. return fd.format(time.getTime())
  253. + String.format("temperature:%1$.1f humidity:%2$.2f",
  254. temperature, humidity);
  255. }
  256.  
  257. }
  258.  
  259. // //
  260. private Calendar lastTime = Calendar.getInstance();
  261. {
  262. // adjust data to the half hour 调整数据到半个小时
  263. lastTime.set(Calendar.MINUTE, 30);
  264. lastTime.set(Calendar.SECOND, 00);
  265. }
  266.  
  267. private float lastTemp = 65.0f;//
  268. private int tempDirection = +1;//温度 方位
  269. private float lastHumidity = 50.0f;//最后的 湿度
  270. private int humidityDirection = +1;//湿气 方位
  271. private Random rand = new Random(47);
  272. List<DataPoint> data = Collections
  273. .synchronizedList(new ArrayList<DataPoint>());
  274.  
  275. //被调度的任务,它在每次运行时。都能够产生仿真数据,并将其加入到Greenhouse的list<DataPoint>中
  276. // ineer class
  277. class CollectData implements Runnable
  278. {
  279.  
  280. @Override
  281. public void run()
  282. {
  283. System.out.println("CollectData》》》run");
  284. synchronized (GreenhouseScheduler.this)
  285. {
  286. // pretend the interval is longer than it is: 假装间隔时间比是:
  287. lastTime.set(Calendar.MINUTE,
  288. lastTime.get(Calendar.MINUTE) + 30);
  289. // one in 5 chances of reversing the direction:一个在5 扭转方向的机会:
  290. if (rand.nextInt(5) == 4)
  291. {
  292. tempDirection = -tempDirection;// 方向
  293. }
  294. // store previous value: 店前一个值:
  295. lastTemp = lastTemp + tempDirection * (1.0f + rand.nextFloat());
  296. if (rand.nextInt(5) == 4)
  297. {
  298. humidityDirection = -humidityDirection;
  299.  
  300. }
  301. lastHumidity = lastHumidity + humidityDirection
  302. * rand.nextFloat();
  303. // calendar must be cloned , otherwise all
  304. // dataPoints hold references to the same lastTime.
  305. // for a basic object like calendar,clone() is ok.
  306. data.add(new DataPoint((Calendar) lastTime.clone(), lastTemp,
  307. lastHumidity));
  308.  
  309. }
  310.  
  311. }
  312.  
  313. }
  314.  
  315. // //////////////main
  316. public static void main(String[] args)
  317. {
  318. GreenhouseScheduler gh = new GreenhouseScheduler();
  319.  
  320. //延迟多少时间 关闭
  321. gh.scheduler(gh.new Terminate(), 5000);
  322.  
  323. // former restart class not necessary:前重新启动类没有必要:
  324. gh.repeat(gh.new Bell(), 0, 1000);//响铃
  325. gh.repeat(gh.new ThermostatNight(), 0, 2000);//夜晚 2秒运行
  326.  
  327. gh.repeat(gh.new LightOn(), 0, 200);//灯
  328. gh.repeat(gh.new LightOff(), 0, 400);
  329.  
  330. gh.repeat(gh.new WaterOn(), 0, 600);//水
  331. gh.repeat(gh.new WaterOff(), 0, 800);
  332. //
  333. gh.repeat(gh.new ThernostatDay(), 0, 1400);//白天
  334. gh.repeat(gh.new CollectData(), 500, 500);
  335.  
  336. }
  337. }
  338. /***
  339. * output:
  340. * Bing!响铃>>
  341. 自己主动控温器 夜晚设置
  342. 打开电灯
  343. 旋转 关灯
  344. 温室水开
  345. 温室水关
  346. 温室白天 设置
  347. 打开电灯
  348. 打开电灯
  349. 旋转 关灯
  350. CollectData》》》run
  351. 温室水开
  352. 打开电灯
  353. 打开电灯
  354. 旋转 关灯
  355. 温室水关
  356. Bing!响铃>>
  357. 打开电灯
  358. CollectData》》》run
  359. 打开电灯
  360. 温室水开
  361. 旋转 关灯
  362. 打开电灯
  363. 温室白天 设置
  364. CollectData》》》run
  365. 打开电灯
  366. 温室水关
  367. 旋转 关灯
  368. 打开电灯
  369. 温室水开
  370. Bing!响铃>>
  371. CollectData》》》run
  372. 旋转 关灯
  373. 打开电灯
  374. 自己主动控温器 夜晚设置
  375. 打开电灯
  376. 打开电灯
  377. 旋转 关灯
  378. 温室水开
  379. 温室水关
  380. CollectData》》》run
  381. 打开电灯
  382. 打开电灯
  383. 旋转 关灯
  384. 温室白天 设置
  385. 打开电灯
  386. CollectData》》》run
  387. 温室水开
  388. Bing!响铃>>
  389. 旋转 关灯
  390. 温室水关
  391. 打开电灯
  392. 打开电灯
  393. CollectData》》》run
  394. 旋转 关灯
  395. 温室水开
  396. 打开电灯
  397. 打开电灯
  398. Bing!响铃>>
  399. 自己主动控温器 夜晚设置
  400. 旋转 关灯
  401. 温室水关
  402. CollectData》》》run
  403. 打开电灯
  404. 打开电灯
  405. 温室水开
  406. 温室白天 设置
  407. 打开电灯
  408. 旋转 关灯
  409. CollectData》》》run
  410. 打开电灯
  411. 打开电灯
  412. 温室水关
  413. 温室水开
  414. 旋转 关灯
  415. Bing!响铃>>
  416. 打开电灯
  417. CollectData》》》run
  418. Terminate》》结束
  419. DataPoint:2015/07/19 09:00 00temperature:66.4 humidity:50.05
  420. DataPoint:2015/07/19 09:30 00temperature:68.0 humidity:50.47
  421. DataPoint:2015/07/19 10:00 00temperature:69.7 humidity:51.42
  422. DataPoint:2015/07/19 10:30 00temperature:70.8 humidity:50.87
  423. DataPoint:2015/07/19 11:00 00temperature:72.0 humidity:50.32
  424. DataPoint:2015/07/19 11:30 00temperature:73.2 humidity:49.92
  425. DataPoint:2015/07/20 12:00 00temperature:71.9 humidity:49.81
  426. DataPoint:2015/07/20 12:30 00temperature:70.1 humidity:50.25
  427. DataPoint:2015/07/20 01:00 00temperature:68.9 humidity:51.00
  428. DataPoint:2015/07/20 01:30 00temperature:67.7 humidity:50.21
  429.  
  430. */

java并发 使用ScheduledExecutor的温室控制器--thinking in java 21.7.5的更多相关文章

  1. 《Java并发编程实战》第十六章 Java内存模型 读书笔记

    Java内存模型是保障多线程安全的根基,这里不过认识型的理解总结并未深入研究. 一.什么是内存模型,为什么须要它 Java内存模型(Java Memory Model)并发相关的安全公布,同步策略的规 ...

  2. java 并发原子性与易变性 来自thinking in java4 21.3.3

    java 并发原子性与易变性  具体介绍请參阅thinking in java4 21.3.3 thinking in java 4免费下载:http://download.csdn.net/deta ...

  3. java并发编程(十五)----(线程池)java线程池简介

    好的软件设计不建议手动创建和销毁线程.线程的创建和销毁是非常耗 CPU 和内存的,因为这需要 JVM 和操作系统的参与.64位 JVM 默认线程栈是大小1 MB.这就是为什么说在请求频繁时为每个小的请 ...

  4. 【搞定 Java 并发面试】面试最常问的 Java 并发基础常见面试题总结!

    本文为 SnailClimb 的原创,目前已经收录自我开源的 JavaGuide 中(61.5 k Star![Java学习+面试指南] 一份涵盖大部分Java程序员所需要掌握的核心知识.欢迎 Sta ...

  5. 【搞定 Java 并发面试】面试最常问的 Java 并发进阶常见面试题总结!

    本文为 SnailClimb 的原创,目前已经收录自我开源的 JavaGuide 中(61.5 k Star![Java学习+面试指南] 一份涵盖大部分Java程序员所需要掌握的核心知识.觉得内容不错 ...

  6. Java并发编程的艺术笔记(五)——Java中的锁

    一.Lock接口的几个功能: 显示的获取和释放锁 尝试非阻塞的获取锁 能被中断的获取锁 超时获取锁 使用方式: Lock lock = new ReentrantLock(); lock.lock() ...

  7. 《Java并发编程的艺术》第5章 Java中的锁 ——学习笔记

    参考https://www.cnblogs.com/lilinzhiyu/p/8125195.html 5.1 Lock接口 锁是用来控制多个线程访问共享资源的方式. 一般来说一个锁可以防止多个线程同 ...

  8. 转:【Java并发编程】之十六:深入Java内存模型——happen-before规则及其对DCL的分析(含代码)

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/17348313 happen-before规则介绍 Java语言中有一个"先行发生 ...

  9. 那些年读过的书《Java并发编程实战》十、再探究Java内存模型

    1.什么是内存模型,为什么需要它? (1)内存模型的发展背景 近几年计算性能通过重排序实现了很大的提升,而且处理器也越来越朝着多核处理器发展以实现硬件的并行性.随着处理器的不断强大,编译器也在不断的改 ...

随机推荐

  1. Error -27791: Server xx has shut down the connection prematurely

    最近在进行一次性能测试中遇到一个问题,并发较大的时候会出现LR出现Error -27791: Server xx has shut down the connection prematurely的ER ...

  2. [codility]Equi-leader

    http://codility.com/demo/take-sample-test/equileader 一开始想到从左和右两边开始扫取众数,但求众数又要重新扫一遍,这样复杂度就是O(n^2)了.此题 ...

  3. thinkphp 模板替换

    具体详见tp手册. 如果需要修改模板替换映射路径. 则需: 'TMPL_PARSE_STRING'=>array( '__PUBLIC__'=>__ROOT__.'/'.APP_NAME. ...

  4. Autodesk 2014全套密钥

    AUTODESK 2014通用安装序列号: 666-69696969 667-98989898 400-45454545 066-66666666 AUTODESK 2014全系列产品密钥 001F1 ...

  5. Android 自定义ToggleButton+用SharedPreferences保存用户配置

    布局文件:   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" androi ...

  6. VC多文档编程技巧(取消一开始时打开的空白文档)

    VC多文档编程技巧(取消一开始时打开的空白文档) http://blog.csdn.net/crazyvoice/article/details/6185461 VC多文档编程技巧(取消一开始时打开的 ...

  7. Android 编译大全

    http://quanminchaoren.iteye.com/blog/840917

  8. scaleform 注意事项

    在使用 自带的UI .fla 里面的组建时 需要把自己建立的fla进行如下设置.  文件-发布设置-flash-脚本actionscript3.0设置——舞台:自动声明舞台实例    

  9. MySQL 内存监控

    上一篇blog介绍了因为sql查询information_schema表而导致内存暴涨的case. 今天顺便做了一个thd内存的监控: 先来介绍下MySQL的内存: 1. 线程内内存:thd-> ...

  10. Html——footer的使用

    html部分 <div class="container"> <div class="body"></div> <di ...