zoukankan      html  css  js  c++  java
  • 使用JUC实现定时任务

    第一种:使用Executors.newScheduledThreadPool实现定时任务

    这种实现方式是阿里规范不推荐的使用方式,有资源耗尽的风险

    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.ScheduledFuture;
    
    import static java.util.concurrent.TimeUnit.SECONDS;
    
    /**
     * @author Created by niugang on 2019/10/17/14:40
     */
    public class ScheduledExecutorTest {
    
        public static void main(String[] args) {
            ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    
            final Runnable beeper = new Runnable() {
                @Override
                public void run() {
                    System.out.println("beep");
                }
            };
            /**
             * initialDelay:首次执行延迟的时间
             * period:连续执行之间的一段时间
             */
            final ScheduledFuture<?> beeperHandle =
                    scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
    
            scheduler.schedule(new Runnable() {
    
                @Override
                public void run() {
                    System.out.println("任务取消了");
                    beeperHandle.cancel(true);
                }
            }, 60, SECONDS);
        }
    }
    
    

    第二种:使用ScheduledThreadPoolExecutor实现定时任务

    推荐的方式

    import com.google.common.util.concurrent.ThreadFactoryBuilder;
    
    import java.util.Date;
    import java.util.concurrent.ScheduledThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
    
    /**
     * @author Created by niugang on 2019/10/17/15:10
     */
    public class ScheduledThreadPoolTest {
    
     
        private static ScheduledThreadPoolExecutor executor;
    
        static {
            executor = new ScheduledThreadPoolExecutor(5, new ThreadFactoryBuilder()
                    .setNameFormat("Scheduled-thread").build());
    
        }
    
        public static void main(String[] args) {
            ScheduledFuture<?> scheduledFuture = executor.scheduleAtFixedRate(
                    new Runnable() {
                        @Override
                        public void run() {
                            System.out.println("Execute work task:" + new Date());
                        }
                    }  , 10, 10, TimeUnit.SECONDS);
            System.out.println(scheduledFuture.isCancelled());
             //scheduledFuture.isDone() 如果此任务已完成,则返回true。完成可能是由于正常的终止、异常或取消——在所有这些情况下,此方法都将返回true。
            //scheduledFuture.isCancelled()) 如果此任务在正常完成之前被取消,则返回true
            executor.schedule(new Runnable() {
                @Override
                public void run() {
                    System.out.println("取消任务");
                    //尝试停止所有正在执行的任务,停止等待任务的处理,并返回等待执行的任务列表。
                    scheduledFuture.cancel(true);
                    System.out.println(scheduledFuture.isCancelled());
                }
            }, 60, TimeUnit.SECONDS);
    
    
        }
    
    
    }
    
    

    第三种:SpringBoot中定时任务的创建方式

    方式一:使用spring提供的注解
    方式二:基于ThreadPoolTaskScheduler
    @SpringBootApplication
    @Slf4j
    public class DemoApplication implements ApplicationRunner {
    
        @Autowired
        private ThreadPoolTaskScheduler threadPoolTaskScheduler;
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
        @Override
        public void run(ApplicationArguments args) {
    
            threadPoolTaskScheduler.scheduleAtFixedRate(() -> log.info("SPRING BOOT SCHEDULE"), new Date(),5000);
        }
    
        @Bean
        public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
            return new ThreadPoolTaskScheduler();
        }
    }
    

    第四种:使用Quartz

    先使用jdk自带的,如果满足不了在使用spring提供的,如果在还满足不了可以借助三方插件

    微信公众号
    JAVA程序猿成长之路

  • 相关阅读:
    hugeng007_SupportVectorMachine_demo
    hugeng007_RandomForestClassifier_demo
    hugeng007_pca_vs_Ida_demo
    hugeng007_Muti-Layer Perceptron_demo
    hugeng007_LogisticRegression_demo
    hugeng007_adaboost_demo
    渗透测试第三章web安全基础--web系统框架
    渗透测试第二章---网络协议安全
    渗透测试第一章 信息收集--- 扫描技术与抓包分析
    爬虫公开课学习的一天
  • 原文地址:https://www.cnblogs.com/niugang0920/p/12187014.html
Copyright © 2011-2022 走看看