我个人感觉国外与国内的教育有些不一样,大家觉的自己行的可以试试
a) Explain what you understand by the term monitor in relation to concurrent systems.  Briefly outline the facilities provided by Java for implementing monitors.
(5 s)b)  A home heating system contains the following objects: An instance of class TemperatureReader; this regularly reads the room temperature (of classTemperature) from a thermometer and writes this value into an object of class Pool An instance of class Pool, which provides storage and access methods for the latest temperature read by TemperatureReader.  Pool only holds a single value. An instance of class Controller which regularly takes the latest temperature from the Pool, and uses it to calculate and execute appropriate adjustments to the boiler settings. This Controller object can have its operation suspended if its turnOff() method is called, and reactivated if its turnOn() method is called.  An instance of class WatchDog which regularly compares the latest temperature in the Pool against an upper threshold value. If the temperature exceeds this threshold, the WatchDog reports this in a log file, and suspends the operation of the Controller object (by calling its turnOff method) until the temperature falls below a second (lower) threshold, when it reactivates the Controller (by calling its turnOn method).. In these questions, assume that threads should sleep for one second between execution of repeated actions, and that the main loop in a thread should run indefinitely (i.e. do not worry about terminating threads). i. Draw a UML communication (object collaboration) diagram for this system. (2 s) ii.  Write code for the Pool class, explaining how its clients use it.  Note: the getTemperature method in Pool is a non-destructive read, so the same value may be read more than once.
(3 s) iii.  Write code for the main routine in the WatchDog class, assuming that this has references to the Pool and Controller objects.
(4 s) iv..  Write outline code for the whole of the Controller class, assuming that a reference to the Pool object is passed to it in its constructor.
(6 s)Notes:  We do not expect you to submit executable code for this question, just Java pseudocode demonstrating that you understand how Java threads work.  You do not need to include exception handlers.  

Do NOT use the deprecated methods stop, suspend and resume from java.lang.Thread.

解决方案 »

  1.   

    国内java,教的是语言本身,国外是实现面向对象的方法。记得老师出的题就是
    用swing写一个程序来实现+-*/的calc。
    汗!
      

  2.   

    class TemperatureReader extends Thread{
        private Pool pool;
        private int interval;
        TemperatureReader(Pool pool,int interval){
            this.pool=pool;
            this.interval=interval;
        }
        private double getTemperature(){
            System.out.println("read value from the thermometer ");
            //return value from the thermometer 
            return 0.0;
        }
        public void run(){
            while (true){
                double degree=getTemperature();
                Temperature t=new Temperature();
                synchronized (pool){
                    pool.setTemperature(t);
                }
                try{
                    this.sleep(interval);  //block thread 1 second
                }catch(Exception ex){
                    ex.printStackTrace();
                }
            }
        }
    }class Temperature {
        private double degree;
        Temperature(){
            degree=0;
        }
        Temperature(double degree){
            this.degree=degree;
        }    public void setDegree(double degree){
            this.degree=degree;
        }
        public double getDegree(){
            return this.degree;
        }
    }class Pool {
        private Temperature temperature;
        public void setTemperature(Temperature temperature){
            this.temperature=temperature;
        }
        public Temperature getTemperature(){
            return this.temperature;
        }
    }/**
     An instance of class Controller which regularly takes the latest temperature 
     from the Pool, and uses it to calculate and execute appropriate adjustments to 
     the boiler settings. This Controller object can have its operation suspended 
     if its turnOff() method is called, and reactivated if its turnOn() method is 
     called.
     */
    class Controller extends Thread {
        private Pool pool;
        private int interval;
        private boolean onoff=false;
        Controller(Pool pool,int interval){
            this.pool=pool;
            this.interval=interval;
            onoff=true;
        }
        private void adjustBoiler(Temperature t){
            System.out.println(" adjustments to the boiler settings ");
        }
        public synchronized void turnOn(){
            if (onoff==false){
                System.out.println("turn on the Controller and boiler");
                onoff = true;
            }
        }
        public synchronized void turnOff(){
            if (onoff){
                System.out.println("turn off the Controller and boiler");
                onoff = false;
            }
        }
        
        public void run(){
            while(true){
                synchronized (this){
                    if (onoff){
                        Temperature t=null;
                        synchronized (pool){
                            t=pool.getTemperature();
                        }
                        adjustBoiler(t);
                    }
                }
                try{
                    this.sleep(this.interval);
                }catch(Exception ex){
                    ex.printStackTrace();
                }
            }
        }
    }/*
     An instance of class WatchDog which regularly compares the latest temperature 
     in the Pool against an upper threshold value. If the temperature exceeds this 
     threshold, the WatchDog reports this in a log file, and suspends the operation 
     of the Controller object (by calling its turnOff method) until the temperature 
     falls below a second (lower) threshold, when it reactivates the Controller 
     (by calling its turnOn method)..
    */
    public class WatchDog extends Thread {
        private Pool pool;
        private Controller controller;
        private double upperthreshold;
        private double lowerthreshold;
        private int interval;
        public WatchDog(Pool pool,Controller controller,double upperthreshold,
                        double lowerthreshold,int interval) {
            this.controller=controller;
            this.pool=pool;
            this.upperthreshold=upperthreshold;
            this.lowerthreshold=lowerthreshold;
            this.interval=interval;
        }
        private void writeLog(String s){
            System.out.println("write log: "+s);
        }
        public void run(){
            while (true){
                try{
                    this.sleep(interval);
                }catch(Exception ex){
                    ex.printStackTrace();
                }
                Temperature t=null;
                synchronized(pool){
                    t=pool.getTemperature();
                }
                if (t.getDegree()>upperthreshold){
                    synchronized(controller){
                        controller.turnOff();
                    }
                    writeLog(new java.util.Date().toString()+t.getDegree());
                } else if (t.getDegree()<lowerthreshold){
                    synchronized(controller){
                        controller.turnOn();
                    }
                }
            }
        }
        
        public static void main(String[] args) {
            Pool pool=new Pool();
            TemperatureReader tr=new TemperatureReader(pool,1000);
            tr.start();
            
            Controller ctrl=new Controller(pool,900);
            ctrl.start();
            
            WatchDog dog=new WatchDog(pool,ctrl,30,20,800);
            dog.start();
        }
    }
      

  3.   

    a)请谈谈你怎么理解术语“监视器”与并发系统的关系。简要的列出由Java提供的实现监视器的组件。(5分)
    b)一个室内供热系统包含如下对象:
    一个TemperatureReader类的实例:它周期性地从一个温度计上读取室内温度(Temperature类),并将其写入Pool类的一个对象中。
    一个Pool类的实例:它为由TemperatureReader读取的最新温度值提供存储和访问的方法。Pool只保存单个值。
    一个Controller类的实例:它周期性地从Pool中取最新的温度值,并进行计算,然后对加热器的设置进行适当的调节。如果它的turnOff()方法被调用,将暂停它的操作,而当turonOn()方法被调用时将会恢复。
    一个WatchDog类的实例:它周期性地对最新温度值和上限值进行比较。如果温度超过了这个上限,WatchDog在一个日志文件里报告,并且暂停Controller对象的操作(调用turnOff()方法),直到温度降到上限值以下,它才恢复Controller(调到turnOn()方法)。在上面的问题中,假设所有线程在执行重复的操作时,中间休息一秒种,而且线程中的主循环是无限执行的(也就是不用关心线程的终止)。i.为这个系统画一个UML通信图。
    (2分)ii.写出Pool类的代码来描述它的客户如何使用它。注意:Pool中的getTemperature()方法是非破坏性读取,所以同样的值可以被读取多次。
    (3分)iii.写出WatchDog类主要功能的代码,假设它有Pool和Controller对象的引用。
    (4分)iv.大概地写出整个Controller类的代码,假设对Pool对象的引用是通过构造函数来传递。
    (6分)注意:我们并不希望你提交这个问题的可执行代码,只要Java伪代码来描绘你对Java线程机制的理解。你不需要把异常处理包含进来。不要使用来自java.lang.Thread的stop(),suspend()和resume()的过时方法。
      

  4.   

    谢谢xiao7cn的翻译,期待更加完美的答案
      

  5.   

    Godfinger(no1dog):
    每个人出的题目都有他的上下文环境。
    这个题目肯定有它的上下文环境,可能是出题人讲过的一个专题。他的答案我也许知道,但我现在不知道他是说什么,也就是说他的说法跟我的知识库匹配不起来
      

  6.   

    To:quanquan626(圈圈)这里还有道补考题目。
    大家应该都懂吧
    An application running on a network client displays a regularly-updated list of stock price information, received from a server. The design of the client application includes the following classes:&#8226; Stock – this class has attributes including a unique identifier for the company and stock, itslatest price, the time it was last updated, the number held, and so on.&#8226; StockData – this stores a collection of Stock objects, representing the user's portfolio.&#8226; StockUpdater – this receives messages over the network containing a list of stock prices.  It retrieves each Stock object in the list from the collection in StockData, and updates it.&#8226; ScreenView – each time the StockData is updated, an object of this classdisplays the fresh data on the screen (showing stock code, description, price, etc).  The ScreenView class supports a number of different display formats (e.g. in a grid, in graphical form, etc.) It may be necessary to add new formats.&#8226; LogFile - each time the StockData is updated, an object of this class uses the fresh data to update a record of the stock information held on disk. Show how the following design patterns could be used in the design of this system:i) Observer  (8 s)
    ii) Singleton  (4 s)
    iii) Iterator  (4 s) In your answer, for each of these patterns:&#8226; Describe its purpose.
    &#8226; Show where it could be used and explain why its use is appropriate here.
    &#8226; Describe its structure, giving a UML class diagram (where appropriate) and outline Java code for its use in this system (where Java provides direct support for the pattern, you should use it).
    其实那里的教育我真的感觉不一样,考试可以带回家做。好像很少有闭卷考试。都是些设计
      

  7.   

    一个运行在网络上的客户端程序,显示从服务器发来的周期性更新的股价信息。这个客户端程序的设计包含如下类:
    Stock类:此类具有这个公司和股票的唯一标识、最新价格、最新更新时间、持有数量等等。
    StockData类:它保存一组Stock对象,来表示用户的证券清单。
    StockUpdater类:它从网络上接收一系列的股价信息,然后从StockData组中逐个提取Stock对象,并更新它。
    ScreenView类:每当StockData被更新时,这个类的对象将更新的数据在屏幕上表示出来(显示股票代码、描述、价格等等)。ScreenView类技术多种不同的显示格式(比如网格、图形表示),且可能允许增加新的格式。
    LogFile类:每当StockData被更新时,这个类的对象用最新数据来更新磁盘上的股票信息。请说明如何使用如下几种设计模式来设计这个系统:
    i) Observer (观察者模式)
    ii) Singleton  (单态模式)
    iii) Iterator  (跌代器模式)在你的回答中,对于每一种模式:
    1)描述它的用途。
    2)说明它应该用在哪里,为什么它在那里的作用是合适的?
    3)描述它的结构,给出一个UML类图(在合适的地方), 并用大致的Java代码来说明它在系统中的作用(在Java提供了对该模式直接支持的地方,你也可以使用它)。
      

  8.   

    那道Java的模式设计没人愿意试试看吗
      

  9.   

    其实这门课虽然是java程序设计,但是重点不在语法上
      

  10.   

    一个运行在网络上的客户端程序,显示从服务器发来的周期性更新的股价信息。这个客户端程序的设计包含如下类:
    Stock类:此类具有这个公司和股票的唯一标识、最新价格、最新更新时间、持有数量等等。
    StockData类:它保存一组Stock对象,来表示用户的证券清单。
    StockUpdater类:它从网络上接收一系列的股价信息,然后从StockData组中逐个提取Stock对象,并更新它。
    ScreenView类:每当StockData被更新时,这个类的对象将更新的数据在屏幕上表示出来(显示股票代码、描述、价格等等)。ScreenView类支持多种不同的显示格式(比如网格、图形表示),且可能允许增加新的格式。
    LogFile类:每当StockData被更新时,这个类的对象用最新数据来更新磁盘上的股票信息。请说明如何使用如下几种设计模式来设计这个系统:
    i)Observer (观察者模式)
    ii)Singleton  (单态模式)
    iii)Iterator  (跌代器模式)在你的回答中,对于每一种模式:
    1)描述它的用途。
    2)说明它应该用在哪里,为什么它在那里的作用是合适的?
    3)描述它的结构,给出一个UML类图(在合适的地方), 并用大致的Java代码来说明它在系统中的作用(在Java提供了对该模式直接支持的地方,你也可以使用它)。