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!執行了0
Thermostat to night setting執行了0
Turning on lights執行了0
Bing!執行了1
Thermostat to night setting執行了1
Bing!執行了2
Bing!執行了3
Turning on lights執行了1
Bing!執行了4
Thermostat to night setting執行了2
Bing!執行了5
Bing!執行了6
Thermostat to night setting執行了3
Turning on lights執行了2
Terminating
Bing!執行了7
nowww 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章