import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class X {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Timer timer = new Timer();
MyTask task = new MyTask();
MyTask task1 = new MyTask();
timer.schedule(task1, 0, 2000);
Thread.sleep(5000);
task1.cancel();
System.out.println("----------------------------------");
timer.schedule(task.getCopy(), 5000, 1000);
}
static class MyTask extends TimerTask implements Cloneable {
private Xxx x;
public MyTask() {
super();
x = new Xxx();
x.name = "aaaaaaaaaaaa";
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName() + "\t" + new Date() + "\t" + x.name + "\t" + x.toString());
}
public MyTask getCopy() {
try {
MyTask m = (MyTask)this.clone();
// m.x = this.x;
System.out.println(m.x == this.x);
System.out.println(m == this);
return m;
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
static class Xxx {
String name;
}
}
java Timer cancel的例子
最新推荐文章于 2022-11-02 01:10:40 发布