多线程-传统定时任务

Posted newlangwen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程-传统定时任务相关的知识,希望对你有一定的参考价值。

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);
    }

}

 

以上是关于多线程-传统定时任务的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 定时+多线程执行

JAVA多线程提高一:传统线程技术&传统定时器Timer

(黑马Java多线程与并发库高级应用)02 传统定时器技术回顾

Spring Boot 定时任务单线程和多线程

java定时任务使用多线程webservcie执行了两次这是为啥?

C#中定时器执行定时器触发任务是单线程还是多线程?