不是每隔2S执行一次吗,这么执行了两三次就没了
MyTimerTask 的run 方法里面有执行次数的判断,大于三次就cancel 掉了task
if (count <= 3) {
System.out.println("Current exec name is : " + name);
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Current exec time is: " + sFormat.format(calendar.getTime()));
count ++;
}else {
cancel();
System.out.println("task Cancel");
}
前面在mytimertask类中加了if判断,执行次数大于3次后调用了cancel方法停止了任务