傳統定時器使用


2s後打印 接着4s後打印  再接着2s後打印  再接着4s後打印如此循環java

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TraditionalTimerTest {
    static int count=0;
    public static void main(String[] args) {
        
        class MyTimerTask extends TimerTask {
          
            @Override
            public void run() {
                count=(count+1)%2;
                System.out.println("bombing!");
                
                 new Timer().schedule(new MyTimerTask(),2000+2000*count);

            }
        }
        
        
        new Timer().schedule(new MyTimerTask(),2000);
        
        
        while(true){
            System.out.println(new Date().getSeconds());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            
                e.printStackTrace();
            }
            
        }
    }
}



quartz定時器框架框架

週一週五的天天晚上凌晨三點半收郵件,週六週日不收郵件,須要使用quartz
ide

相關文章
相關標籤/搜索