- Mvcs.ctx.getDefaultIoc();在此servlet实例建的任务中有效
new Thread(new Runnable() {//启动定时器线程
@Override
public void run() {
while(true) {
Calendar date = Calendar.getInstance();
int hour = date.get(Calendar.HOUR_OF_DAY);
//while(hour==0) {
synchronized(this) {
Ioc ioc = Mvcs.ctx.getDefaultIoc();
StaticpParamService staticpParamService = ioc.get(StaticpParamService.class);
int count = staticpParamService.count();
staticpParamService.staticDetail();
System.out.println("测试定时器呀呀呀呀呀呀"+count);
//}
}
try {
Thread.sleep(3600);
} catch (InterruptedException e) {
e.printStackTrace();
}//睡眠250毫秒
}
}
}).start(); - Mvcs.ctx.getDefaultIoc();在此TimerTask子类实例的run方法中无效@Override
public void run() {
// TODO Auto-generated method stub
Ioc ioc = Mvcs.ctx.getDefaultIoc();
StaticpParamService staticpParamService = ioc.get(StaticpParamService.class);
int count = staticpParamService.count();
staticpParamService.staticDetail();
System.out.println("测试定时器呀呀呀呀呀呀"+count);
System.out.println("66666666666666666666666666666");
System.out.println(name);
}
如下是测试单元
@Test
public void test() {
Calendar date = Calendar.getInstance();
int hour = date.get(Calendar.HOUR_OF_DAY);
System.out.println(hour);
Timer timer = new Timer();
MyTimerTask myTimerTask = new MyTimerTask("我是ximen");
timer.schedule(myTimerTask, 2);
System.out.println("fafafafaufasa");
}
//注释掉TimerTask子类实例的run方法中的 Mvcs.ctx.getDefaultIoc();获取的实例和方法,可以执行System.out.println("66666666666666666666666666666");
System.out.println(name);否则不可以执行
8 回复
用try-catch包起来, 打印出错信息,才知道原因的
try {
// TODO Auto-generated method stub
Ioc ioc = Mvcs.ctx.getDefaultIoc();
StaticpParamService staticpParamService = ioc.get(StaticpParamService.class);
int count = staticpParamService.count();
staticpParamService.staticDetail();
System.out.println("测试定时器呀呀呀呀呀呀"+count);
System.out.println("66666666666666666666666666666");
System.out.println(name);
}
catch (Throwable e) {
e.printStackTrace();
}
new Thread(new Runnable() {//启动定时器线程
@Override
public void run() {
while(true) {
Calendar date = Calendar.getInstance();
int hour = date.get(Calendar.HOUR_OF_DAY);
//while(hour==0) {
synchronized(this) {
Ioc ioc = Mvcs.ctx.getDefaultIoc();
StaticpParamService staticpParamService = ioc.get(StaticpParamService.class);
int count = staticpParamService.count();
staticpParamService.staticDetail();
System.out.println("测试定时器呀呀呀呀呀呀"+count);
//}
}
try {
Thread.sleep(3600);
} catch (InterruptedException e) {
e.printStackTrace();
}//睡眠250毫秒
}
}
}).start();
@Override
public void run() {
// TODO Auto-generated method stub
Ioc ioc = Mvcs.ctx.getDefaultIoc();
StaticpParamService staticpParamService = ioc.get(StaticpParamService.class);
int count = staticpParamService.count();
staticpParamService.staticDetail();
System.out.println("测试定时器呀呀呀呀呀呀"+count);
System.out.println("66666666666666666666666666666");
System.out.println(name);
}
package com.cms.special.common;
import java.util.Calendar;
import java.util.Timer;
import org.junit.Test;
public class Tests {
@Test
public void test() {
Calendar date = Calendar.getInstance();
int hour = date.get(Calendar.HOUR_OF_DAY);
System.out.println(hour);
Timer timer = new Timer();
MyTimerTask myTimerTask = new MyTimerTask("我是ximen");
timer.schedule(myTimerTask, 2);
System.out.println("fafafafaufasa");
}
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
Ioc ioc = Mvcs.ctx.getDefaultIoc();
StaticpParamService staticpParamService = ioc.get(StaticpParamService.class);
int count = staticpParamService.count();
staticpParamService.staticDetail();
System.out.println("测试定时器呀呀呀呀呀呀"+count);
System.out.println("66666666666666666666666666666");
System.out.println(name);
}catch(Exception e) {
e.printStackTrace();
}
}
添加回复
请先登陆