Java多線程並發管理,java多線程並發

來源:互聯網
上載者:User

Java多線程並發管理,java多線程並發

   在書上看到了一個好方法,當多個線程並發時,可以用scheduleAtFixedRate來管理,scheduleAtFixedRate定時執行一次任務,是重複執行,而ScheduledThreadPoolExecutor將只執行一次任務,
  如果你有多個任務,同時進行,並且,是定時的執行,那麼以下的程式,完全可以滿足你的要求:

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;}     //運行一次任務ScheduledThreadPoolExecutor 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 定時執行一次任務,是重複執行scheduler.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"+"執行了"+(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 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"+"執行了"+(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!"+"執行了"+(j++));}}
class Terminate implements Runnable {public void run() {System.out.println("Terminating");scheduler.shutdownNow(); //結束線程// Must start a separate task to do this job,// since the scheduler has been shut down:new Thread() {public void run() {         //最後一次執行System.out.println("nowww");}}.start();}}

public static void main(String[] args) {TestGreenhouseScheduler gh = new TestGreenhouseScheduler();gh.schedule(gh.new Terminate(), 1000*7); //7秒運行一次任務,這裡只執行一次
gh.repeat(gh.new Bell(), 0, 1000);//1秒執行一次任務,是重複執行gh.repeat(gh.new ThermostatNight(), 0, 2000);gh.repeat(gh.new LightOn(), 0, 3000);}}  
---結果7秒之後,結束所有的線程,你看Bing執行了7次---
Bing!執行了0Thermostat to night setting執行了0Turning on lights執行了0Bing!執行了1Thermostat to night setting執行了1Bing!執行了2Bing!執行了3Turning on lights執行了1Bing!執行了4Thermostat to night setting執行了2Bing!執行了5Bing!執行了6Thermostat to night setting執行了3Turning on lights執行了2TerminatingBing!執行了7nowww 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.