zoukankan      html  css  js  c++  java
  • Java中四种创建定时任务的方式

    在开发中,创建定时任务的方式有很多,下面简单介绍四种常见的方式:Runnable,TimerTask,线程池ScheduledExecutorService,Quartz。

    1.使用Runnable

        private static void testRunnable() {
            final long timeInterval = 1000;
    
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        System.out.println("Hello, XXX " + Thread.currentThread());
                        try {
                            Thread.sleep(timeInterval);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
    
            Thread thread = new Thread(runnable);
            thread.start();
        }

    2.使用TimerTask

    private static void testTimerTask() {
            TimerTask timerTask = new TimerTask() {
                @Override
                public void run() {
                    System.out.println("Hello, " + Thread.currentThread());
                }
            };
    
            Timer timer = new Timer();
            timer.scheduleAtFixedRate(timerTask, 0, 1500);
        }

    3.使用线程池ScheduledExecutorService

        private static void testExecutorService() {
            long initialDelay = 0;
            long period = 1;
            TimeUnit unit = TimeUnit.SECONDS;
    
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    System.out.println("Hello, " + Thread.currentThread());
                }
            };
    
            ScheduledExecutorService service = Executors
                    .newSingleThreadScheduledExecutor();
            service.scheduleAtFixedRate(runnable, initialDelay, period, unit);
        }

    4.使用Quartz

        private static void testQuartz() throws SchedulerException {
            JobDetail jobDetail = JobBuilder.newJob(MySimpleJob.class).withIdentity("job1", "group1").build();
            //创建触发器,每5秒钟执行一次
            Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1")
            .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever())
            .build();
    
            SchedulerFactory schedulerFactory = new StdSchedulerFactory();
            Scheduler scheduler = schedulerFactory.getScheduler();
    
            //将任务及其触发器放入调度器
            scheduler.scheduleJob(jobDetail, trigger);
            //调度器开始调度任务
            scheduler.start();
        }

    MySimpleJob如下:

    package com.li.test;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    
    /*
     * 实现了org.quartz.Job接口的类
     * 不要和TestTask写在一个文件夹里面,否则execute不会运行。
     */
    
    public class MySimpleJob implements org.quartz.Job {
    
        public MySimpleJob() {
        }
    
        public void execute(JobExecutionContext context)
                throws JobExecutionException {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS"); 
            System.out.println("MySimpleJob execute-->" + sdf.format(new Date()));
        }
    }
    
  • 相关阅读:
    分化Oracle数据库日记文件(1)
    ORACLE稀有错误代码的阐发与经管(二)
    Oracle暗码文件的运用和维护
    在ORACLE中移动数据库文件
    ORACLE8的分区管理
    Oracle中如何间接运转OS号令(上)
    Oracle数据库平安计谋阐明 (三)
    Oracle7.X 回滚表空间数据文件误删除措置举动措施
    Oracle功用究极优化 中
    网络知识爆炸的年代~如何更好地学习吸收有用的知识
  • 原文地址:https://www.cnblogs.com/lishbo/p/9955994.html
Copyright © 2011-2022 走看看