首页 > 编程语言 > 详细

多线程-传统定时任务

时间:2017-12-27 13:04:49      阅读:161      评论:0      收藏:0      [点我收藏+]
package com.thread.timer;

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

/**
 * 间隔2执行一次再隔4秒执行一次
 * 
 *
 */
public class TraditionalTimerTest {

    public static void main(String[] args) throws InterruptedException {
        Timer timer = new Timer();
        long cur = System.currentTimeMillis();
        timer.schedule(new MyTimerTask(), 2000);
        while (true) {
            System.out.println(new Date().getSeconds());
            Thread.sleep(1000);
        }
    }

}

class MyTimerTask extends TimerTask {
    private static int count = 0;

    @Override
    public void run() {
        count = (count + 1) % 2;
        // TODO Auto-generated method stub
        System.out.println("bombing!!");
        
        new Timer().schedule(new MyTimerTask(), 2000+2000*count);
    }

}

 

多线程-传统定时任务

原文:https://www.cnblogs.com/newlangwen/p/8124965.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!