java编程思想 并发 21.7.5

问题在代码注释中,谢谢大佬

//: concurrency/GreenhouseScheduler.java
package concurrency; /* Added by Eclipse.py */

// Rewriting innerclasses/GreenhouseController.java
// to use a ScheduledThreadPoolExecutor.
// {Args: 5000}

import java.util.concurrent.*;
import java.util.*;

public class GreenhouseScheduler {
    private volatile boolean light = false;
    private volatile boolean water = false;
    private String thermostat = "Day";
    ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(10);

    public synchronized String getThermostat() {
        return thermostat;
    }

    public synchronized void setThermostat(String value) {
        thermostat = value;
    }



    public void schedule(Runnable event, long delay) {
        scheduler.schedule(event, delay, TimeUnit.MILLISECONDS);
    }

    public void repeat(Runnable event, long initialDelay, long period) {
        scheduler.scheduleAtFixedRate(event, initialDelay, period, TimeUnit.MILLISECONDS);
    }

    class LightOn implements Runnable {
        public void run() {
            // Put hardware control code here to
            // physically turn on the light.
            System.out.println("Turning on lights");
            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 {
        public void run() {
            // Put hardware control code here.
            System.out.println("Thermostat to night setting");
            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 {
        public void run() {
            System.out.println("Bing!");
        }
    }

    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() {
                    for (DataPoint d : data)
                        System.out.println(d);
                }
            }.start();
        }
    }

    // New feature: data collection
    static class DataPoint {
        final Calendar time;
        final float temperature;
        final float humidity;

        public DataPoint(Calendar d, float temp, float hum) {
            time = d;
            temperature = temp;
            humidity = hum;
        }

        public String toString() {
            return time.getTime() + String.format(" temperature: %1$.1f humidity: %2$.2f", temperature, humidity);
        }
    }

    private Calendar lastTime = Calendar.getInstance();
    { // Adjust date to the half hour
        lastTime.set(Calendar.MINUTE, 30);
        lastTime.set(Calendar.SECOND, 00);
    }
    private float lastTemp = 65.0f;
    private int tempDirection = +1;
    private float lastHumidity = 50.0f;
    private int humidityDirection = +1;
    private Random rand = new Random(47);
    List<DataPoint> data = Collections.synchronizedList(new ArrayList<DataPoint>());

    class CollectData implements Runnable {
        public void run() {
            System.out.println("Collecting data");
            synchronized (GreenhouseScheduler.this) {  
        //  ****************************************************************

            //这里使用GreenhouseScheduler.this与this 有区别吗

            //***************************************************************
                // Pretend the interval is longer than it is:
                lastTime.set(Calendar.MINUTE, lastTime.get(Calendar.MINUTE) + 30);
                // One in 5 chances of reversing the direction:
                if (rand.nextInt(5) == 4)
                    tempDirection = -tempDirection;
                // Store previous value:
                float nextFloat = rand.nextFloat();
                System.out.println(nextFloat);
                lastTemp = lastTemp + tempDirection * (1.0f + nextFloat);
                if (rand.nextInt(5) == 4)
                    humidityDirection = -humidityDirection;
                lastHumidity = lastHumidity + humidityDirection * rand.nextFloat();
                // Calendar must be cloned, otherwise all
                // DataPoints hold references to the same lastTime.
                // For a basic object like Calendar, clone() is OK.
                data.add(new DataPoint((Calendar) lastTime.clone(), lastTemp, lastHumidity));
            }
        }
    }

    public static void main(String[] args) {
        GreenhouseScheduler gh = new GreenhouseScheduler();
        gh.schedule(gh.new Terminate(), 5000);
        // Former "Restart" class not necessary:
        gh.repeat(gh.new Bell(), 0, 1000);
        gh.repeat(gh.new ThermostatNight(), 0, 2000);
        gh.repeat(gh.new LightOn(), 0, 200);
        gh.repeat(gh.new LightOff(), 0, 400);
        gh.repeat(gh.new WaterOn(), 0, 600);
        gh.repeat(gh.new WaterOff(), 0, 800);
        gh.repeat(gh.new ThermostatDay(), 0, 1400);
        gh.repeat(gh.new CollectData(), 500, 500);
    }
} /* (Execute to see output) */// :~

3个回答

Java 并发编程运行时多任务调试是难点,需要理清楚流程。
这个代码有点复杂,需要花点时间看,如果你相信我可以先点采纳,明天周末花点时间给看看。

caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复请你们大家多多姿瓷我菠菜菌: 人家不回答你的问题,你要反思下,是不是不积极主动采纳了,不愿意采纳,没人会回答你的问题的
大约一个月之前 回复
dabocaiqq
请你们大家多多姿瓷我菠菜菌 回复毕小宝: 不是啊,我不是参加比赛,而是最近学了java python,很多知识点不懂,要专家辅导下
大约一个月之前 回复
wojiushiwo945you
毕小宝 回复请你们大家多多姿瓷我菠菜菌: 你是不是在帮大 Y 刷题啊,你采纳帮我增加了太多分了,我不好意思再回答你的问题了啊。
大约一个月之前 回复
dabocaiqq
请你们大家多多姿瓷我菠菜菌 太不公平了,小宝老师,为什么不回答我的问题
大约一个月之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 是的,这种问题特别费工夫,采纳一下举手之劳,相信专家一定会给你满意答案的。
大约一个月之前 回复

代码没具体功能没有去看,但是你的问题可以解答,区别绝对是有的,使用this指CollectData实例,代码中的方式指的是GreenhouseScheduler实例,锁持有者不同

this表示这个类
你在CollectData类里面写this当然是CollectData了,所以必须指定GreenhouseScheduler.this

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!