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(); } }
请说出原题,别说自己的理解。
可能简单就是直接写两个继承Thread的类,不行的话最复杂救治Timer类。
帮阿海顶一下
这个用Timer类实现可不可以,在TimerTask处理另的那个线程??
呵呵 不要见笑 我不太懂
sleep+wait+noityall
这道题应该是测试线程之间的消息传递和控制的吧楼主是这个意思?
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();
}
}