Java uses ScheduledExecutor's greenhouse controller concurrently -- thinking in java 21.7.5

Source: Internet
Author: User
Tags dateformat

Java uses ScheduledExecutor's greenhouse controller concurrently -- 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;/*** greenhouse controller * @ author lenovo **/public class GreenhouseScheduler {private volatile boolea N light = false; // light private volatile boolean water = false; // water private String thermostat = "Day"; // automatic thermostat public synchronized String getThermostat () {return thermostat;} public synchronized void setThermostat (String thermostat) {this. thermostat = thermostat;} // scheduler ScheduledThreadPoolExecutor schedtor = new ScheduledThreadPoolExecutor (10);/***** @ param event * @ param delay latency */public void scheduler (Runnable event, long delay) {/*** creates and executes one-time operations enabled after a given delay. */Scheduler. schedule (event, delay, TimeUnit. MILLISECONDS );} /*** repeat * @ param envent * @ param initialDelay * @ param period: the shorter the execution cycle, the faster the execution. */public void repeat (Runnable envent, long initialDelay, long period) {/*** creates and executes a scheduled operation that is enabled for the first time after a given initial delay. Subsequent operations have a given cycle; that is, the execution will start after initialDelay *, and then after initialDelay + period, and then after initialDelay + 2 * period, and so on. */Scheduler. scheduleAtFixedRate (envent, initialDelay, period, TimeUnit. MILLISECONDS );} /*** inner class ** turn on the light */class LightOn implements Runnable {// put hardware control code here to put the hardware control code here // physically turn on the light. turn on the body light. @ Overridepublic void run () {// System. out. println ("Turning on lights"); System. out. println ("turn on the light"); light = true ;}} /*** off * @ author lenovo **/class LightOff implements Runnable {// put hardware control code here to put the hardware control code here // physically turn off the light. turn off the light. @ Overridepublic void run () {System. out. println ("turn off the light"); // System. out. println ("Turning off light"); water = true ;}} class wateon implements Runnable {@ Overridepublic void run () {// System. out. println ("Turning greenhouse water on"); System. out. println ("greenhouse water open"); water = true ;}} class WaterOff implements Runnable {@ Overridepublic void run () {System. out. println ("greenhouse water mark"); // System. out. println ("Turning greenhouse wat Er off "); water = false ;}}/*** temperature controller night * @ author lenovo **/class ThermostatNight implements Runnable {@ Overridepublic void run () {// put hardware control code here put the hardware control code here // System. out. println ("thermostat to night setting"); System. out. println ("Automatic Temperature Controller Night settings"); setThermostat ("Night ");}} /*** daytime ** @ author lenovo **/class ThernostatDay implements Runnable {@ Overridepublic void run () {// put hardware contr Ol code hereSystem. out. println ("greenhouse daytime Settings"); // System. out. println ("thermostat to day setting"); setThermostat ("Day ");}} /*** Bell * @ author lenovo **/class Bell implements Runnable {@ Overridepublic void run () {System. out. println ("Bing! Bell> ") ;}}/*** Terminate * @ 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 Self-scheduler has disabled new Thread () {public void run () {for (DataPoint d: data) {System. out. println ("DataPoint:" + d );}};}. start () ;}}/*** can hold and display a single data segment * @ 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 adjust the data to the lastTime of half an hour. set (Calendar. MINUTE, 30); lastTime. set (Calendar. SECOND, 00);} private float lastTemp = 65.0f; // private int tempDirection = + 1; // temperature range private float lastHumidity = 50366f; // The Last humidity private int humidityDirection = + 1; // moisture orientation private Random rand = new Random (47); List
 
  
Data = Collections. synchronizedList (new ArrayList
  
   
(); // The scheduled task that generates simulation data each time it runs and adds it to the Greenhouse list.
   
    
// Ineer classclass CollectData implements Runnable {@ Overridepublic void run () {System. out. println ("CollectData" "run"); synchronized (greenhousescheded. this) {// pretend the interval is longer than it is: pretend the interval is: lastTime. set (Calendar. MINUTE, lastTime. get (Calendar. MINUTE) + 30); // one in 5 chances of reversing the direction: an opportunity to reverse the direction at 5: if (rand. nextInt (5) = 4) {tempDirection =-tempDirection; // Toward} // store previous value: the value before the store: 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 (Cale Ndar) lastTime. clone (), lastTemp, lastHumidity) ;}}/// // mainpublic static void main (String [] args) {GreenhouseScheduler gh = new GreenhouseScheduler (); // how long is the latency to disable gh. scheduler (gh. new Terminate (), 5000); // former restart class not necessary: the pre-restart class is not required: gh. repeat (gh. new Bell (), 0, 1000); // Bell gh. repeat (gh. new ThermostatNight (), 0, 2000); // gh is executed 2 seconds at night. repeat (gh. new LightOn (), 0,200); // light gh. repeat (Gh. new LightOff (), 0,400); gh. repeat (gh. new wateon (), 0,600); // water gh. repeat (gh. new WaterOff (), 0,800); // gh. repeat (gh. new ThernostatDay (), 0, 1400); // daytime gh. repeat (gh. new CollectData (), 500,500) ;}/ ***** output: * Bing! The Automatic Temperature Controller sets the night to turn on the electric light rotation turn off the light greenhouse water turn on the greenhouse water turn off the greenhouse during the day set to turn on the electric light turn off the light CollectData, run the greenhouse water turn on the light turn on the electric light turn off the light Greenhouse water off Bing! Ringing> turn on the lights CollectData "" run turn on the lights greenhouse water turn Turn on the lights greenhouse daytime set CollectData "" run turn on the lights greenhouse water turn Turn off the lights turn on the lights greenhouse water turn on Bing! Bell> CollectData "" run turn off the light turn on the electric light automatic temperature controller at night set on the electric light turn on the light turn off the light greenhouse water turn on the greenhouse water turn off CollectData "run turn on the electric light turn off the light greenhouse daytime set the electric lights to CollectData, run, greenhouse, water, and Bing! Ringing> rotating turn off the light greenhouse water turn on the light CollectData "" run rotating turn off the light greenhouse water turn on the light Bing! Ringing> automatic thermostat night setting rotation turn off the light greenhouse water off CollectData "" run turn on the light greenhouse water turn on the greenhouse during the day set turn on the light turn off CollectData "" run turn on the light Greenhouse water off greenhouse water turn Turn off the light Bing! Ring tones> turn on the lights CollectData, runTerminate, and end DataPoint: 2015/07/19 00 temperature: 66.4 humidity: 50.05 DataPoint: 2015/07/19 temperature: 68.0 humidity: 50.47 DataPoint: temperature: 69.7 humidity: 51.42 DataPoint: 00 temperature: 70.8 humidity: 50.87 DataPoint: 00 temperature: 72.0 humidity: 50.32 DataPoint: 00 temperature: 73.2 humidity: 49.92 DataPoint: 00 temperature: 71.9 humidity: 49.81 DataPoint: 00 temperature: 70.1 humidity: 50.25 DataPoint: 00 temperature: 68.9 humidity: 51.00 DataPoint: 00 temperature: 67.7 humidity: 50.21 */
   
  
 


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.