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 Boolean light = false;//Optical Private Volat Ile Boolean water = false;//Water private String thermostat = "Day";//own active thermostat public synchronized String Getthermostat () {retur n Thermostat;} Public synchronized void Setthermostat (String thermostat) {this.thermostat = thermostat;} Scheduler Scheduledthreadpoolexecutor Scheduler = new Scheduledthreadpoolexecutor,/** * * @param event * @param delay Delay */public void Scheduler (Runnable event, long delay) {/** * builds and runs a one-time operation enabled after a given delay. */scheduler.schedule (event, delay, timeunit.milliseconds);} /** * Repeat * @param envent * @param initialdelay * @param the cycle time between successive runs of periodThe faster you run less */public void repeat (Runnable envent, long initialdelay, long period) {/** * Creates and runs a recurring action that is first enabled after a given initial delay. Perhaps the operation has a given period. That is, it will start running after the InitialDelay *. Then run after the Initialdelay+period. Then run after InitialDelay + 2 * period, and so on. */scheduler.scheduleatfixedrate (Envent, InitialDelay, period,timeunit.milliseconds);} /** * Inner class * Turn on Lights */class Lighton implements runnable{//put hardware control code here to put hardware control codes here//physically Turn on the light. Body turn on the light.
@Overridepublic void Run () {//system.out.println ("Turning on Lights"); System.out.println ("Turn on the lamp"); light = True;}} /** * off * @author Lenovo * */class Lightoff implements runnable{//put hardware control code here to put hardware control codes here//physical Ly turn off the light. Turned off the light. @Overridepublic void Run () {System.out.println ("Turn Off Light"),//system.out.println ("Turning off"); water = True;}} Class Wateron 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 off");//system.out.println (" Turning greenhouse water Off "); water = false;}} /** * Thermostat Night * @author Lenovo * */class Thermostatnight implements runnable{@Overridepublic void Run () {//Put hardware Co Ntrol code here//SYSTEM.OUT.PRINTLN the hardware control codes ("Thermostat to Night setting"); System.out.println ("Self-active thermostat night setting"); Setthermostat ("Night");}} /** * Daytime * @author Lenovo * */class Thernostatday IMPlements runnable{@Overridepublic void Run () {//Put hardware control code HERESYSTEM.OUT.PRINTLN ("Greenhouse daytime setting");// System.out.println ("Thermostat to Day Setting"), Setthermostat ("Day");}} /** * Clock * @author Lenovo * */class Bell implements runnable{@Overridepublic void Run () {System.out.println ("bing! bells >> ");}} /** * Termination * @author Lenovo * */class Terminate implements runnable{@Overridepublic void Run () {System.out.println ("Terminate Scheduler.shutdown ();//must start a separate task to does this job must start a separate task to do the job//since the scheduler has been Shut down since the scheduler has closed the new Thread () {public void run () {for (Datapoint d:data) {System.out.println ("Datapoint:" +d);}};}. Start ();}} /** * capable of holding and displaying individual data segments * @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 tem Perature, 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 adjusted for half an hour Lasttime.set (Calendar.minute, +); Lasttime.set (Calendar.second, 00);} private float lasttemp = 65.0f;//private int tempdirection = +1;//temperature azimuth private float lasthumidity = 50.0f;//last Humidity private int humiditydirection = +1;//Moisture azimuth private random rand = new Random (47); list<datapoint> data = collections.synchronizedlist (new arraylist<datapoint> ());//dispatched task, which is run at each time. can generate simulation data and add it to Greenhouse's list<datapoint>//Ineer Classclass Collectdata implements runnable{@ overridepublic void Run () {System.out.println ("Collectdata" "Run"); synchronized (greenhousescheduler.this) {// Pretend the interval is longer than it is: pretend time interval ratio is: Lasttime.set (Calendar.minute,lasttime.get (calendar.minUTE) +//One in 5 chances of reversing the direction: a chance to reverse direction in 5: if (Rand.nextint (5) = = 4) {tempdirection =-tempdire ction;//Direction}//Store Previous value: Pre-store value: lasttemp = lasttemp + tempdirection * (1.0f + rand.nextfloat ()); if (rand.nextin T (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) L Asttime.clone (), lasttemp,lasthumidity));}}} Mainpublic static void Main (string[] args) {greenhousescheduler gh = new Greenhousescheduler ();//delay How much time Close Gh.scheduler (gh.new Terminate (), ();//Former restart class not necessary: The pre-restart class is not necessary: Gh.repeat (Gh.new Bell (), 0, 1000);//Bell Gh.repeat (Gh.new thermostatnight (), 0, 2000);//night 2 seconds Run Gh.repeat (gh.new Lighton (), 0, 200);//Lamp Gh.repeat ( Gh.new Lightoff (), 0, Gh.repeat (gh.new Wateron (), 0, 600);/Water Gh.repeat (gh.new wateroff (), 0, +/-Gh.repeat (Gh.new thernostatday (), 0, 1400);//Daytime Gh.repeat (Gh.new collectdata ( ), 500, 500);}} /*** * Output: * bing! Bell >> own active thermostat night set turn off light green greenhouse water open greenhouse water switch greenhouse day set open electric light turn off light collectdata "" Run greenhouse water open electric light turn off light greenhouse water Close bing! Bell >> turn on the light Collectdata "" Run turn on the electric light greenhouse water turn off the lights turn off the light bulb greenhouse daytime setting collectdata "Run turn on the electric light greenhouse water turn off the light turn off the lamp greenhouse water Open bing! Bell > >collectdata "" Run turn off the lamp turn off the light on its own active thermostat night set turn on the electric light turn off the light greenhouse water open greenhouse water off Collectdata "" Run turn on the electric light turn off the light greenhouse daytime set turn on the lamp collectdata "" Run Greenhouse water bing! Bell >> turn off light greenhouse water off turn on lights Collectdata "" Run turn off light greenhouse water Open electric light bing! ring >> Self active thermostat night set Turn off light Greenhouse water Collectdata "" Run turn on the light bulb open the lamp greenhouse water open greenhouse daytime set turn off light collectdata "" Run turn on the lights turn on the lights greenhouse water-off greenhouse water turn off the light bing! Bell >> Turn on the Lights Collectdata "" Runterminate "" End Datapoint:2015/07/19 00temperature:66.4 humidity:50.05datapoint:2015/07/ 09:30 00temperature:68.0 humidity:50.47datapoint:2015/07/19 10:00am 00temperature:69.7 humidity:51.42DataPoint:2015 /07/19 00temperature:70.8 HUMIDITY:50.87DATAPOINT:2015/07/1911:00am 00temperature:72.0 humidity:50.32datapoint:2015/07/19 00temperature:73.2 humidity:49.92datapoint:2015/ 07/20 00temperature:71.9 humidity:49.81datapoint:2015/07/20 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 concurrency using Scheduledexecutor's greenhouse controller--thinking in Java 21.7.5