Java multi-thread concurrency management and java multi-thread concurrency

Source: Internet
Author: User

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

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.