Java实现定时任务的几种方案

使用java.util.Timer类

package haiyang.yu.timer;

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

/**
 * Created on 2018-04-20 13:09
 * <p>Title:  haiyang.yu.timer</p>
 * <p>Description: </p>
 *
 * @author <a href="mailto:991138518@qq.com">yuhaiyang</a>
 * @version 1.0
 */
public class TimedTask {

    private static void useTimerImplTimedTask(){

        // 第一个参数是任务,第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间,时间单位是毫秒
        new Timer().scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                System.out.println("Local Time is " + new Date().toString());
            }
        },0L,1000L);
    }


    public static void main(String[] args) {
        useTimerImplTimedTask();
    }
}

使用java.util.concurrent.ScheduledExecutorService

package haiyang.yu.timer;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created on 2018-04-20 13:09
 * <p>Title:  haiyang.yu.timer</p>
 * <p>Description: </p>
 *
 * @author <a href="mailto:991138518@qq.com">yuhaiyang</a>
 * @version 1.0
 */
public class TimedTask {

    private static void userScheduledExecutorServiceImplTiemdTask(){
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("Local Time is " + new Date().toString());
            }
        };
        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        // 第一个参数是任务,第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间,第四个参数是时间单位
        service.scheduleAtFixedRate(runnable, 0L, 1L, TimeUnit.SECONDS);
    }

    public static void main(String[] args) {
        userScheduledExecutorServiceImplTiemdTask();
    }
}

不建议使用这种方案,最好是自己去创建线程池。可以参考下面的实现方案。

使用java.util.concurrent.ScheduledThreadPoolExecutor结合org.apache.commons.lang3.concurrent.BasicThreadFactory(推荐使用)

package haiyang.yu.timer;

import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import java.util.Date;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created on 2018-04-20 13:09
 * <p>Title:  haiyang.yu.timer</p>
 * <p>
     使用这种方案,需要引入common-lang3的jar包
     <dependency>
         <groupId>org.apache.commons</groupId>
         <artifactId>commons-lang3</artifactId>
         <version>3.6</version>
     </dependency>
 * </p>
 *
 * @author <a href="mailto:991138518@qq.com">yuhaiyang</a>
 * @version 1.0
 */
public class TimedTask {

    private static void useScheduledThreadPoolExecutorImplTimedTask(){
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(
                1, new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(false).build());
        // 第一个参数是任务,第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间,第四个参数是时间单位
        scheduledThreadPoolExecutor.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("Local Time is " + new Date().toString());
            }
        }, 0L, 1L, TimeUnit.SECONDS);
    }

    public static void main(String[] args) {
        useScheduledThreadPoolExecutorImplTimedTask();
    }
}

使用java.lang.Thread

package haiyang.yu.timer;

import java.util.Date;

/**
 * Created on 2018-04-20 13:09
 * <p>Title:  haiyang.yu.timer</p>
 * <p>Description: </p>
 *
 * @author <a href="mailto:991138518@qq.com">yuhaiyang</a>
 * @version 1.0
 */
public class TimedTask {

    private static void useThreadImplTimedTask(){

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while (true) {
                    
                    System.out.println("Local Time is " + new Date().toString());
                    
                    try {
                        //时间间隔,单位是毫秒
                        Thread.sleep(1000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        
        Thread thread = new Thread(runnable);
        thread.start();

    }

    public static void main(String[] args) {
        useThreadImplTimedTask();
    }
}

这种方式使用的最多,但也是最lower的。

转载于:https://my.oschina.net/epoch/blog/1798410

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值