我在 java 中的 ScheduledExecutorService 有问题(几天前我没有遇到这个问题,这让我觉得很奇怪)。请找到下面的代码和控制台输出。重复执行任务之间的延迟是 1 毫秒,所以理想情况下我应该达到每秒 1000 的计数(给或需要几毫秒),但这只是没有发生。请帮忙..
代码:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class SchedulerTest {
private static final ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
static DateFormat df= new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SSS");
private static class TestRunnable1 implements Runnable{
private int count=0;
@Override
public void run() {
count++;
if(count1 % 1000==0){
System.out.println(count1+" "+df.format(Calendar.getInstance().getTime()));
}
}
}
public static void main(String[] args){
Runnable localService = new TestRunnable1();
try{
executor.scheduleWithFixedDelay(localService, 0, 1, TimeUnit.MILLISECONDS);
}catch(Exception e){
e.printStackTrace();
}
}
}
控制台输出
1000 2013 年 3 月 13 日 14:43:54.477 2000 2013 年 3 月 13 日 14:44:10.296 3000 2013 年 3 月 13 日 14:44:26.381 4000 2013 年 3 月 13 日 14:44:42.621 5000 2013 年 3 月 13 日14:44:55.907 6000 2013 年 3 月 13 日 14:44:58.516 7000 2013 年 3 月 13 日 14:45:05.896 8000 2013 年 3 月 13 日 14:45:10.292 9000 2013 年 3 月 13 日 14:00:15.129 10 2013 年 3 月 13 日 14:45:18.187
谢谢, 阿比纳夫