Java Multithreading concurrency Management

Source: Internet
Author: User

See a good method in the book, when multiple threads concurrency, can be used to manage Scheduleatfixedrate, Scheduleatfixedrate scheduled to perform a task, is repeated execution, And Scheduledthreadpoolexecutor will only perform one task,
If you have multiple tasks at the same time, and are scheduled to execute, then 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 task onceScheduledthreadpoolexecutor Scheduler = new Scheduledthreadpoolexecutor (10);
public void Schedule (Runnable event, long delay) {Scheduler.schedule (event, delay, timeunit.milliseconds);}
public void repeat (Runnable event, long initialdelay, long period) {Scheduleatfixedrate scheduled execution of a task, is repeated executionScheduler.scheduleatfixedrate (event, InitialDelay, period,Timeunit.milliseconds);}
Class Lighton implements Runnable {int j=0;public void Run () {Put hardware control code here toPhysically 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 toPhysically turn off the light.System.out.println ("Turning off Lights");Light = false;}}
Class Wateron 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");Scheduler.shutdownnow (); End ThreadMust start a separate task to does this job,Since the scheduler have been shut down:New Thread () {public void Run () {Last executionSystem.out.println ("nowww");}}.start ();}}

public static void Main (string[] args) {Testgreenhousescheduler gh = new Testgreenhousescheduler ();Gh.schedule (Gh.new Terminate (), 1000*7); 7 Seconds to run a task, only once
Gh.repeat (Gh.new Bell (), 0, 1000);//1 seconds perform a task, repeatGh.repeat (Gh.new thermostatnight (), 0, 2000);Gh.repeat (Gh.new Lighton (), 0, 3000);}}
---Results 7 seconds later, end all the threads that you seeBing has performed 7 times---
Bing! performed a 0Thermostat to night setting performed a 0Turning on lights performed 0bing! performed a 1Thermostat to night setting performed 1bing! performed 2bing! Performed a 3Turning on lights performed 1bing! performed a 4Thermostat to night setting performed 2bing! performed 5bing! performed 6Thermostat to Night Setting performed a 3Turning on lights performed the 2terminatingbing! performed 7nowww 

Java Multithreading concurrency Management

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.