Java multi-thread concurrency management and java multi-thread concurrency
A good method is shown in the book. When multiple threads are concurrent, scheduleAtFixedRate can be used for management. scheduleAtFixedRate regularly executes one task, which is a repeated execution, while ScheduledThreadPoolExecutor only executes one task, If you have multiple tasks that are executed at the same time and scheduled, the following programs can fully meet your requirements:
Import java. util. concurrent. *; import java. util .*; Public class TestGreenhouseScheduler {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;} // run the ScheduledThreadPoolExecutor schedtor = new ScheduledThreadPoolExecutor (10 ); Public void schedule (Runnable event, long delay) {schedle. schedule (event, delay, TimeUnit. MILLISECONDS );} Public void repeat (Runnable event, long initialDelay, long period) {// scheduleAtFixedRate: The schediod is executed repeatedly. scheduleAtFixedRate (event, initialDelay, period, TimeUnit. MILLISECONDS );} Class LightOn implements Runnable {int j = 0; public void run () {// Put hardware control code here to // physically turn on the light. system. out. println ("Turning on lights" + "executed" + (j ++); light = true ;}} Class LightOff implements Runnable {public void run () {// Put hardware control code here to // physically turn off the light. system. out. println ("Turning off lights"); light = false ;}} Class wateon implements Runnable {public void run () {// Put hardware control code here. System. out. println ("Turning greenhouse water on"); water = true ;}} Class WaterOff implements Runnable {public void run () {// Put hardware control code here. System. out. println ("Turning greenhouse water off"); water = false ;}} Class ThermostatNight implements Runnable {int j = 0; public void run () {// Put hardware control code here. system. out. println ("Thermostat to night setting" + "executed" + (j ++); setThermostat ("Night ");}} Class ThermostatDay implements Runnable {public void run () {// Put hardware control code here. system. out. println ("Thermostat to day setting"); setThermostat ("Day ");}} Class Bell implements Runnable {int j = 0; public void run () {System. out. println ("Bing! "+" Executed "+ (j ++ ));}} Class Terminate implements Runnable {public void run () {System. out. println ("Terminating"); schedating. shutdownNow (); // end Thread // Must start a separate task to do this job, // since the scheduler has been shut down: new Thread () {public void run () {// last execution of System. out. println ("nowww ");}}. start ();}}
Public static void main (String [] args) {TestGreenhouseScheduler gh = new TestGreenhouseScheduler (); gh. schedule (gh. new Terminate (), 1000*7); // run the task once every 7 seconds. Only once Gh. repeat (gh. new Bell (), 0, 1000); // execute a task in 1 second, which is a repeated gh execution. repeat (gh. new ThermostatNight (), 0, 2000); gh. repeat (gh. new LightOn (), 0, 3000 );}} --- After the result is 7 seconds, all threads are ended. You can see that Bing has executed 7 times --- Bing! 0 Thermostat to night setting is executed and 0 Turning on lights is executed! 1 Thermostat to night setting is executed and 1 Bing is executed! 2 Bing is executed! 3 Turning on lights executed 1 Bing! 4 Thermostat to night setting is executed and 2 Bing is executed! 5 Bing is executed! 6 Thermostat to night setting is executed. 3 Turning on lights is executed. 2 TerminatingBing is executed! Executed 7 nowww
|