to liounzhou(潇潇夜雨) :"TimerTask1被覆盖了"是什么意思?不明白我自己写了个测试,发现又有矛盾了 public class Reminder { Timer timer; public Reminder(int delay,int seconds,String test) { timer = new Timer(); RemindTask task = new RemindTask(); task.setTest(test); timer.schedule(task,delay,seconds*1000); } class RemindTask extends TimerTask { private String test; public void run() { System.out.println(test); }
public void setTest(String test) { this.test = test; } } public static void main(String args[]) { System.out.println("About to schedule task."); new Reminder(1000*5,1,"task 111"); new Reminder(1000*1,1,"task 222"); System.out.println("Task scheduled."); } }得到输出:About to schedule task. Task scheduled. task 222 task 222 task 222 task 222 task 111 task 222 task 111 task 222 task 111 这样看来task是作为独立线程运行的,而且timer调度的时候是并发的。那“timer同一时间只能调度一个task”就有点疑问了。而当task1发生阻塞时,task2能否正常运行这个问题我还不知道如何测试,因为我不知道如何正确的使task1阻塞。我曾经尝试使用以下方法,却得到异常 public class Reminder { Timer timer; public Reminder(int delay,int seconds,String test,long time) { timer = new Timer(); RemindTask task = new RemindTask(); task.setTest(test); task.setTime(time); timer.schedule(task,delay,seconds*1000); } class RemindTask extends TimerTask { private String test; private long time; public void run() { try { this.wait(time); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(test); }
public void setTest(String test) { this.test = test; } public void setTime(long time) { this.time = time; } } public static void main(String args[]) { System.out.println("About to schedule task."); new Reminder(1000*5,1,"task 111",500); new Reminder(1000*1,1,"task 222",0); System.out.println("Task scheduled."); } } 输出:About to schedule task. Task scheduled. java.lang.IllegalMonitorStateException: current thread not owner at java.lang.Object.wait(Native Method) at Reminder$RemindTask.run(Reminder.java:34) at java.util.TimerThread.mainLoop(Timer.java:432) at java.util.TimerThread.run(Timer.java:382) java.lang.IllegalMonitorStateException: current thread not owner at java.lang.Object.wait(Native Method) at Reminder$RemindTask.run(Reminder.java:34) at java.util.TimerThread.mainLoop(Timer.java:432) at java.util.TimerThread.run(Timer.java:382) 还请各位说说为何会出现这种情况
timer.schedule(TimerTask2, seconds*1000);
我觉得应该要看第一个seconds*1000的时间长度,如果足够长,会做完TimerTask1才会继续做TimerTask2.如果时间太短,会直接跳过TimerTask1而去做TimerTask2,因为TimerTask1被覆盖了.如果是在执行TimerTask1的时候,TimerTask2开始执行,则程序会报错.
public class Reminder {
Timer timer; public Reminder(int delay,int seconds,String test) {
timer = new Timer();
RemindTask task = new RemindTask();
task.setTest(test);
timer.schedule(task,delay,seconds*1000);
} class RemindTask extends TimerTask {
private String test;
public void run() {
System.out.println(test);
}
public void setTest(String test) {
this.test = test;
}
} public static void main(String args[]) {
System.out.println("About to schedule task.");
new Reminder(1000*5,1,"task 111");
new Reminder(1000*1,1,"task 222");
System.out.println("Task scheduled.");
}
}得到输出:About to schedule task.
Task scheduled.
task 222
task 222
task 222
task 222
task 111
task 222
task 111
task 222
task 111
这样看来task是作为独立线程运行的,而且timer调度的时候是并发的。那“timer同一时间只能调度一个task”就有点疑问了。而当task1发生阻塞时,task2能否正常运行这个问题我还不知道如何测试,因为我不知道如何正确的使task1阻塞。我曾经尝试使用以下方法,却得到异常
public class Reminder {
Timer timer; public Reminder(int delay,int seconds,String test,long time) {
timer = new Timer();
RemindTask task = new RemindTask();
task.setTest(test);
task.setTime(time);
timer.schedule(task,delay,seconds*1000);
} class RemindTask extends TimerTask {
private String test;
private long time;
public void run() {
try {
this.wait(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(test);
}
public void setTest(String test) {
this.test = test;
}
public void setTime(long time) {
this.time = time;
}
} public static void main(String args[]) {
System.out.println("About to schedule task.");
new Reminder(1000*5,1,"task 111",500);
new Reminder(1000*1,1,"task 222",0);
System.out.println("Task scheduled.");
}
}
输出:About to schedule task.
Task scheduled.
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at Reminder$RemindTask.run(Reminder.java:34)
at java.util.TimerThread.mainLoop(Timer.java:432)
at java.util.TimerThread.run(Timer.java:382)
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at Reminder$RemindTask.run(Reminder.java:34)
at java.util.TimerThread.mainLoop(Timer.java:432)
at java.util.TimerThread.run(Timer.java:382)
还请各位说说为何会出现这种情况
调用wait方法要先获得同步锁,否则就有上面的异常
sychronized(this){
//
this.wait(xx);
}
另外如果需要保证逻辑上的顺序,只要合成为一个task就行了
示例代码
compositeTask extends TimerTask{
public void run(){
Thread task1=new Thread(new Task1());
Thread task2=new Thread(new Task2());
task1.start();
task1.join();//保证task1,结束task2才开始
task2.start();
}
}