题目如下:
编写一个报告从线程执行开始所经历时间的程序,该程序每15秒打印一条消息.消息打印线程由时间打印线程通知.在不改变时间打印线程前提下,添加一个每隔7秒打印另一消息的线程请大虾帮帮我啊,~急要~

解决方案 »

  1.   

    题目应该是很简单。
    请说出原题,别说自己的理解。
    可能简单就是直接写两个继承Thread的类,不行的话最复杂救治Timer类。
      

  2.   

    我来凑热闹 
    帮阿海顶一下
    这个用Timer类实现可不可以,在TimerTask处理另的那个线程??
    呵呵 不要见笑 我不太懂
      

  3.   

    这个要用到多线程!
    sleep+wait+noityall
      

  4.   

    楼主的意思是只有一个时间线程是活动的,另外两个线程只是一个打印消息的机器,到时间了,时间线程把他们唤醒一下打印消息,消息内容为当前时间时间线程的时间,打印完了,再sleep掉.
    这道题应该是测试线程之间的消息传递和控制的吧楼主是这个意思?
      

  5.   

    //打印程序
    public class PringMsg implements Runnable {
        private Runnable timer;
        public PringMsg() {
        }
        
        public void run() {
            while (true) {
                synchronized (this) {
                    try {
                        this.wait();
                    } catch (InterruptedException ex) {
                        //need to deal with
                    }
                }
                printMSG();
            }
        }
        
        public void notityByTimer(Runnable timer) {
            this.timer = timer;
            synchronized (this) {
                this.notify();
            }
        }
        
        private void printMSG() {
            if (timer instanceof Timer1) {
                System.out.println(timer + "通知15秒已到,当前时间是 " + new Date(System.currentTimeMillis()));
            } else {
                System.out.println(timer + "通知7秒已到,当前时间是 " + new Date(System.currentTimeMillis()));
            }
        }}
    //即时程序基类
    public abstract class TimerBase implements Runnable {    private Runnable task;
        
        private long interval;
        
        public TimerBase() {
        }
        
        public TimerBase(long interval) {
            this.interval = interval;
        }
        
        public void setTask(Runnable task) {
            this.task = task;
        }
        
        public Runnable getTask() {
            return task;
        }
        
        public void run() {
            while (true) {
                try {
                    Thread.sleep(interval);
                } catch (InterruptedException ex) {
                }
                process();
            }
        }    
        public abstract void process();
    }//两个即时程序
    public class Timer1 extends TimerBase {    public Timer1(long interval) {
            super(interval);
        }
        
        public void process() {
            ((PringMsg)this.getTask()).notityByTimer(this);
        }
    }public class Timer2 extends TimerBase {    public Timer2(long interval) {
            super(interval);
        }    public void process() {
            
            ((PringMsg)this.getTask()).notityByTimer(this);
        }}//主程序入口
    public class RunDemo {
        public RunDemo() {
        }
        
        public static void main(String[] args) {
            Timer1 timer1 = new Timer1(15 * 1000);
            Timer2 timer2 = new Timer2(7 * 1000);
            PringMsg printmsg = new PringMsg();
            timer1.setTask(printmsg);
            timer2.setTask(printmsg);
            new Thread(timer1).start();
            new Thread(timer2).start();
            new Thread(printmsg).start();
        }
    }
      

  6.   

    用两个java.util.TimerTask和java.util.Timer就解决你上面描述的问题