我想问下·~~Timer在执行固定周期的TimerTask的时候~~~
是对TimerTask的同一对象的重复引用呢?还是每次都new新的对象(TimerTask),再执行?比如说Timer secondTimer=new Timer("SecondTimer");
secondTimer.scheduleAtFixedRate(new TestTask(1), 0, 4000L);
secondTimer.scheduleAtFixedRate(new TestTask(2), 7000L, 7000L);
secondTimer.scheduleAtFixedRate(new TestTask(3), 11000L, 11000L);
//其中TestTask是
public class TestTask extends TimerTask { private int type;
public TestTask(int type) {
// TODO Auto-generated constructor stub
this.type=type;
}
public void run() {
// TODO Auto-generated method stub
if (type==1) {
System.out.println("我4秒执行一次");
}else if (type==2) {
System.out.println("我7秒执行一次");
}else if (type==3) {
System.out.println("我11秒执行一次");
}else {
System.out.println("ERROR");
}
}
}
我想知道·Timer在实现这个的时候~~是相当于
while(到时间了){
new TestTask()
}
还是说~~~
task=new TestTask
while(到时间了){
task.run
}因为我的继承TimerTask 的TestTask中会有一些相关的变量的操作·~所以比较在意这个问题·~
还有就是对于一个由Timer启动的TimerTask,它里面的资源需要同步么?
比如,只针对secondTimer.scheduleAtFixedRate(new TestTask(1), 0, 4000L);
我TestTask中type=1里面的局部变量需要同步么?
请各位大哥大姐给解惑~~
是对TimerTask的同一对象的重复引用呢?还是每次都new新的对象(TimerTask),再执行?比如说Timer secondTimer=new Timer("SecondTimer");
secondTimer.scheduleAtFixedRate(new TestTask(1), 0, 4000L);
secondTimer.scheduleAtFixedRate(new TestTask(2), 7000L, 7000L);
secondTimer.scheduleAtFixedRate(new TestTask(3), 11000L, 11000L);
//其中TestTask是
public class TestTask extends TimerTask { private int type;
public TestTask(int type) {
// TODO Auto-generated constructor stub
this.type=type;
}
public void run() {
// TODO Auto-generated method stub
if (type==1) {
System.out.println("我4秒执行一次");
}else if (type==2) {
System.out.println("我7秒执行一次");
}else if (type==3) {
System.out.println("我11秒执行一次");
}else {
System.out.println("ERROR");
}
}
}
我想知道·Timer在实现这个的时候~~是相当于
while(到时间了){
new TestTask()
}
还是说~~~
task=new TestTask
while(到时间了){
task.run
}因为我的继承TimerTask 的TestTask中会有一些相关的变量的操作·~所以比较在意这个问题·~
还有就是对于一个由Timer启动的TimerTask,它里面的资源需要同步么?
比如,只针对secondTimer.scheduleAtFixedRate(new TestTask(1), 0, 4000L);
我TestTask中type=1里面的局部变量需要同步么?
请各位大哥大姐给解惑~~
解决方案 »
- 请问下怎么用javadoc生成UML类图?
- weblogic中报Error 403--Forbidden错在那?
- 编译时提示出错,请指教
- 构造方法的问题
- 超难问题!也是最常见的问题!著名的异常:java.net.SocketException: Connection reset by peer: socket write error,难道不能避免吗?
- socket通讯中的byte[]数组传送问题!!!!急!!!!!!!!!!!!!!!
- 大家好,求一书“java 实例 技术手册 ”
- 唉/大家好/为什麽我的问题没人理呀/害的我费了好几天时间/全部搞定/还靠自己吧~~~~~~~~~~~~~
- 水用过 anfy team ??
- 前辈求帮助一下,下面的代码为什么会错
- 在网页上显示图片
- jface 界面关闭后程序没有退出
task=new TestTask
while(到时间了){
task.run
}我TestTask中type=1里面的局部变量需要同步么?不需要同步的,因为type属于各自TestTask对象的成员,不会发送互斥。
不存在多线程执行的问题,因此不需要同步
恩·~对于一个Timer启动的任务我明白了~~~还想问下·~我现在写的代码结构是这样的一个有声明Timer的执行类 main,从库表获取的多条定时记录(多于5条),假设有5种时间类型(年月日时分)。我定义了一个Timer实现1分钟执行一次,其他时间类型我使用计数器的方式~~
一个extends TimerTask的TestTask,内部实现了5种定时信息的处理方法。也就是说·~我程序启动之后,一条记录会对应一个TestTask
这样的话·~~多条记录如果是同类型的~~~那么他们之间应该不能做成同步的吧?
是不是说我的那段执行代码不能调用全局变量呢?