- import java.util.Calendar;
- import java.util.Date;
- import java.util.Timer;
- import java.util.TimerTask;
- public class Test9 {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- WorkTask workTask=new WorkTask();
- WordTask wordTask=new WordTask();
-
- Timer boss=new Timer();
- //这个任务设定了,为什么没有先开始,而是下面的先开始
- boss.schedule(wordTask, 1000, 1000);
-
- Calendar calendar=Calendar.getInstance();
- calendar.set(2004, 9,10,19,30);
- long start=calendar.getTimeInMillis();
-
- Date date=new Date(start);
- //下面的这个方法是设定在date这天开始任务,这个任务如果不结束的话,为什么上面的任务不会被执行
- boss.scheduleAtFixedRate(workTask, date, 2000);
- System.out.println("任务布置完成");
-
- calendar.set(2004, 9,19,10,20,50);
- long end=calendar.getTimeInMillis();
-
-
- //下面这个又有什么左右哩
- try {
- Thread.sleep(end-start);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- class WorkTask extends TimerTask{
- int i=0;
-
- @Override
- public void run() {
- // TODO Auto-generated method stub
- i++;
- System.out.println("*");
- if(i>=12){
- System.out.println("END");
- cancel();
- }
- }
- }
- class WordTask extends TimerTask{
- @Override
- public void run() {
- // TODO Auto-generated method stub
- System.out.println("A");
- }
-
- }
复制代码 |