乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Spring Boot 定時(shí)任務(wù)單線(xiàn)程和多線(xiàn)程

       XSMforever 2019-05-20

      Spring Boot 的定時(shí)任務(wù):

      第一種:把參數(shù)配置到.properties文件中:

      代碼:

      1. package com.accord.task;
      2. import java.text.SimpleDateFormat;
      3. import java.util.Date;
      4. import org.springframework.scheduling.annotation.Scheduled;
      5. import org.springframework.stereotype.Component;
      6. /**
      7. * 從配置文件加載任務(wù)信息
      8. * @author 王久印
      9. * 2018年3月1日
      10. */
      11. @Component
      12. public class ScheduledTask {
      13. private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
      14. //@Scheduled(fixedDelayString = "${jobs.fixedDelay}")
      15. @Scheduled(fixedDelayString = "2000")
      16. public void getTask1() {
      17. System.out.println("任務(wù)1,從配置文件加載任務(wù)信息,當(dāng)前時(shí)間:" + dateFormat.format(new Date()));
      18. }
      19. @Scheduled(cron = "${jobs.cron}")
      20. public void getTask2() {
      21. System.out.println("任務(wù)2,從配置文件加載任務(wù)信息,當(dāng)前時(shí)間:" + dateFormat.format(new Date()));
      22. }
      23. }
      application.properties文件:
      1. jobs.fixedDelay=5000
      2. jobs.cron=0/5 * * * * ?

      SpringBootCron2Application.java中:

      1. package com.accord;
      2. import org.springframework.boot.SpringApplication;
      3. import org.springframework.boot.autoconfigure.SpringBootApplication;
      4. import org.springframework.scheduling.annotation.EnableScheduling;
      5. @SpringBootApplication
      6. @EnableScheduling
      7. public class SpringBootCron2Application {
      8. public static void main(String[] args) {
      9. SpringApplication.run(SpringBootCron2Application.class, args);
      10. }
      11. }

      注:@EnableScheduling  這個(gè)一定要加上;否則,不會(huì)定時(shí)啟動(dòng)任務(wù)!

      @Scheduled中的參數(shù)說(shuō)明:
      1. @Scheduled(fixedRate=2000):上一次開(kāi)始執(zhí)行時(shí)間點(diǎn)后2秒再次執(zhí)行;
      2. @Scheduled(fixedDelay=2000):上一次執(zhí)行完畢時(shí)間點(diǎn)后2秒再次執(zhí)行;
      3. @Scheduled(initialDelay=1000, fixedDelay=2000):第一次延遲1秒執(zhí)行,然后在上一次執(zhí)行完畢時(shí)間點(diǎn)后2秒再次執(zhí)行;
      4. @Scheduled(cron="* * * * * ?"):按cron規(guī)則執(zhí)行。
      在線(xiàn)Cron表達(dá)式生成器:http://cron./

      第二種定時(shí)任務(wù):?jiǎn)尉€(xiàn)程和多線(xiàn)程

      1、創(chuàng)建定時(shí)任務(wù):

      1. package com.accord.task;
      2. import org.slf4j.Logger;
      3. import org.slf4j.LoggerFactory;
      4. import org.springframework.scheduling.annotation.Scheduled;
      5. import org.springframework.stereotype.Component;
      6. /**
      7. * 構(gòu)建執(zhí)行定時(shí)任務(wù)
      8. * @author 王久印
      9. * 2018年3月1日
      10. * TODO
      11. */
      12. @Component
      13. public class ScheduledTask2 {
      14. private Logger logger = LoggerFactory.getLogger(ScheduledTask2.class);
      15. private int fixedDelayCount = 1;
      16. private int fixedRateCount = 1;
      17. private int initialDelayCount = 1;
      18. private int cronCount = 1;
      19. @Scheduled(fixedDelay = 5000) //fixedDelay = 5000表示當(dāng)前方法執(zhí)行完畢5000ms后,Spring scheduling會(huì)再次調(diào)用該方法
      20. public void testFixDelay() {
      21. logger.info("===fixedDelay: 第{}次執(zhí)行方法", fixedDelayCount++);
      22. }
      23. @Scheduled(fixedRate = 5000) //fixedRate = 5000表示當(dāng)前方法開(kāi)始執(zhí)行5000ms后,Spring scheduling會(huì)再次調(diào)用該方法
      24. public void testFixedRate() {
      25. logger.info("===fixedRate: 第{}次執(zhí)行方法", fixedRateCount++);
      26. }
      27. @Scheduled(initialDelay = 1000, fixedRate = 5000) //initialDelay = 1000表示延遲1000ms執(zhí)行第一次任務(wù)
      28. public void testInitialDelay() {
      29. logger.info("===initialDelay: 第{}次執(zhí)行方法", initialDelayCount++);
      30. }
      31. @Scheduled(cron = "0 0/1 * * * ?") //cron接受cron表達(dá)式,根據(jù)cron表達(dá)式確定定時(shí)規(guī)則
      32. public void testCron() {
      33. logger.info("===initialDelay: 第{}次執(zhí)行方法", cronCount++);
      34. }
      35. }

      使用 @Scheduled來(lái)創(chuàng)建定時(shí)任務(wù) 這個(gè)注解用來(lái)標(biāo)注一個(gè)定時(shí)任務(wù)方法。 
      通過(guò)看 @Scheduled源碼可以看出它支持多種參數(shù):
          (1)cron:cron表達(dá)式,指定任務(wù)在特定時(shí)間執(zhí)行;
          (2)fixedDelay:表示上一次任務(wù)執(zhí)行完成后多久再次執(zhí)行,參數(shù)類(lèi)型為long,單位ms;
          (3)fixedDelayString:與fixedDelay含義一樣,只是參數(shù)類(lèi)型變?yōu)镾tring;
          (4)fixedRate:表示按一定的頻率執(zhí)行任務(wù),參數(shù)類(lèi)型為long,單位ms;
          (5)fixedRateString: 與fixedRate的含義一樣,只是將參數(shù)類(lèi)型變?yōu)镾tring;
          (6)initialDelay:表示延遲多久再第一次執(zhí)行任務(wù),參數(shù)類(lèi)型為long,單位ms;
          (7)initialDelayString:與initialDelay的含義一樣,只是將參數(shù)類(lèi)型變?yōu)镾tring;
          (8)zone:時(shí)區(qū),默認(rèn)為當(dāng)前時(shí)區(qū),一般沒(méi)有用到。

      2、開(kāi)啟定時(shí)任務(wù):
      1. package com.accord;
      2. import org.springframework.boot.SpringApplication;
      3. import org.springframework.boot.autoconfigure.SpringBootApplication;
      4. import org.springframework.scheduling.annotation.EnableScheduling;
      5. @SpringBootApplication
      6. @EnableScheduling
      7. public class SpringBootCron2Application {
      8. public static void main(String[] args) {
      9. SpringApplication.run(SpringBootCron2Application.class, args);
      10. }
      11. }

      注:這里的 @EnableScheduling  注解,它的作用是發(fā)現(xiàn)注解 @Scheduled的任務(wù)并由后臺(tái)執(zhí)行。沒(méi)有它的話(huà)將無(wú)法執(zhí)行定時(shí)任務(wù)。
      引用官方文檔原文:
      @EnableScheduling ensures that a background task executor is created. Without it, nothing gets scheduled.

      3、執(zhí)行結(jié)果(單線(xiàn)程)

      就完成了一個(gè)簡(jiǎn)單的定時(shí)任務(wù)模型,下面執(zhí)行springBoot觀察執(zhí)行結(jié)果:


      從控制臺(tái)輸入的結(jié)果中我們可以看出所有的定時(shí)任務(wù)都是在同一個(gè)線(xiàn)程池用同一個(gè)線(xiàn)程來(lái)處理的,那么我們?nèi)绾蝸?lái)并發(fā)的處理各定時(shí)任務(wù)呢,請(qǐng)繼續(xù)向下看。

      4、多線(xiàn)程處理定時(shí)任務(wù):

      看到控制臺(tái)輸出的結(jié)果,所有的定時(shí)任務(wù)都是通過(guò)一個(gè)線(xiàn)程來(lái)處理的,我估計(jì)是在定時(shí)任務(wù)的配置中設(shè)定了一個(gè)SingleThreadScheduledExecutor,于是我看了源碼,從ScheduledAnnotationBeanPostProcessor類(lèi)開(kāi)始一路找下去。果然,在ScheduledTaskRegistrar(定時(shí)任務(wù)注冊(cè)類(lèi))中的ScheduleTasks中又這樣一段判斷:

      1. if (this.taskScheduler == null) {
      2. this.localExecutor = Executors.newSingleThreadScheduledExecutor();
      3. this.taskScheduler = new ConcurrentTaskScheduler(this.localExecutor);
      4. }
      這就說(shuō)明如果taskScheduler為空,那么就給定時(shí)任務(wù)做了一個(gè)單線(xiàn)程的線(xiàn)程池,正好在這個(gè)類(lèi)中還有一個(gè)設(shè)置taskScheduler的方法:
      1. public void setScheduler(Object scheduler) {
      2. Assert.notNull(scheduler, "Scheduler object must not be null");
      3. if (scheduler instanceof TaskScheduler) {
      4. this.taskScheduler = (TaskScheduler) scheduler;
      5. }
      6. else if (scheduler instanceof ScheduledExecutorService) {
      7. this.taskScheduler = new ConcurrentTaskScheduler(((ScheduledExecutorService) scheduler));
      8. }
      9. else {
      10. throw new IllegalArgumentException("Unsupported scheduler type: " + scheduler.getClass());
      11. }
      12. }
      這樣問(wèn)題就很簡(jiǎn)單了,我們只需用調(diào)用這個(gè)方法顯式的設(shè)置一個(gè)ScheduledExecutorService就可以達(dá)到并發(fā)的效果了。我們要做的僅僅是實(shí)現(xiàn)SchedulingConfigurer接口,重寫(xiě)configureTasks方法就OK了;
      1. package com.accord.task;
      2. import org.springframework.context.annotation.Configuration;
      3. import org.springframework.scheduling.annotation.SchedulingConfigurer;
      4. import org.springframework.scheduling.config.ScheduledTaskRegistrar;
      5. import java.util.concurrent.Executors;
      6. /**
      7. * 多線(xiàn)程執(zhí)行定時(shí)任務(wù)
      8. * @author 王久印
      9. * 2018年3月1日
      10. */
      11. @Configuration
      12. //所有的定時(shí)任務(wù)都放在一個(gè)線(xiàn)程池中,定時(shí)任務(wù)啟動(dòng)時(shí)使用不同都線(xiàn)程。
      13. public class ScheduleConfig implements SchedulingConfigurer {
      14. @Override
      15. public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
      16. //設(shè)定一個(gè)長(zhǎng)度10的定時(shí)任務(wù)線(xiàn)程池
      17. taskRegistrar.setScheduler(Executors.newScheduledThreadPool(10));
      18. }
      19. }
      5、執(zhí)行結(jié)果(并發(fā))


      通過(guò)控制臺(tái)輸出的結(jié)果看出每個(gè)定時(shí)任務(wù)都是在通過(guò)不同的線(xiàn)程來(lái)處理了。

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶(hù) 評(píng)論公約

        類(lèi)似文章 更多