日期:2014-05-19  浏览次数:20898 次

ScheduledThreadPoolExecutor定时问题,求高手!在线等!!!!!!
测试程序:
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
public class test16 
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
ScheduledThreadPoolExecutor timer=new ScheduledThreadPoolExecutor(1);
timer.scheduleAtFixedRate(new TestThread(),0,1000,TimeUnit.MILLISECONDS);
}

}
class TestThread implements Runnable
{
static Logger logger = Logger.getLogger(TestThread.class.getName());
@Override
public void run() 
{
// TODO Auto-generated method stub
logger.info("测试");
}
}

打印的日记:
2012-07-10 10:40:03,156 [TestTask] INFO - 测试
2012-07-10 10:40:04,156 [TestTask] INFO - 测试
2012-07-10 10:40:05,156 [TestTask] INFO - 测试
2012-07-10 10:40:06,156 [TestTask] INFO - 测试
2012-07-10 10:40:07,156 [TestTask] INFO - 测试
2012-07-10 10:40:08,156 [TestTask] INFO - 测试
2012-07-10 10:40:09,156 [TestTask] INFO - 测试
2012-07-10 10:40:10,156 [TestTask] INFO - 测试
2012-07-10 10:40:11,156 [TestTask] INFO - 测试
2012-07-10 10:40:12,156 [TestTask] INFO - 测试
2012-07-10 10:40:13,156 [TestTask] INFO - 测试
2012-07-10 10:40:14,156 [TestTask] INFO - 测试
2012-07-10 10:40:15,156 [TestTask] INFO - 测试
2012-07-10 10:40:16,156 [TestTask] INFO - 测试
2012-07-10 10:40:17,156 [TestTask] INFO - 测试
2012-07-10 10:40:18,156 [TestTask] INFO - 测试
2012-07-10 10:40:19,156 [TestTask] INFO - 测试
2012-07-10 10:40:20,156 [TestTask] INFO - 测试
2012-07-10 10:40:21,156 [TestTask] INFO - 测试
2012-07-10 10:40:22,156 [TestTask] INFO - 测试
2012-07-10 10:40:23,171 [TestTask] INFO - 测试
2012-07-10 10:40:24,171 [TestTask] INFO - 测试
2012-07-10 10:40:25,171 [TestTask] INFO - 测试
2012-07-10 10:40:26,171 [TestTask] INFO - 测试
2012-07-10 10:40:27,171 [TestTask] INFO - 测试
2012-07-10 10:40:28,187 [TestTask] INFO - 测试
2012-07-10 10:40:29,187 [TestTask] INFO - 测试
2012-07-10 10:40:30,187 [TestTask] INFO - 测试
2012-07-10 10:40:31,187 [TestTask] INFO - 测试
2012-07-10 10:40:32,187 [TestTask] INFO - 测试
2012-07-10 10:40:33,187 [TestTask] INFO - 测试
2012-07-10 10:40:34,187 [TestTask] INFO - 测试
2012-07-10 10:40:35,187 [TestTask] INFO - 测试
2012-07-10 10:40:36,187 [TestTask] INFO - 测试
2012-07-10 10:40:37,187 [TestTask] INFO - 测试


想请问下大家,为什么定时不准确,有时候会多15或者16秒,谢谢了


------解决方案--------------------
探讨
想请问下大家,为什么定时不准确,有时候会多15或者16秒,谢谢了

------解决方案--------------------
请理解scheduleAtFixedRate和scheduleWithFixedDelay之间的区别