Spring Boot@Scheduledタイミングタスクの使用
2886 ワード
Spring Boot起動クラス
@EnableScheduling注記を追加する必要があります.
二@Scheduled注記IDタスク
2つのタスクを作成します.いずれのメソッドも実行後1秒に1回実行され、test 02は1分間停止します.
トリプルタスクテスト
Spring Bootアプリケーションを起動し、コンソール出力を確認します.
test 02を実行すると、test 02が実行されるのを待つため、test 01は1分待ってから実行されます.
世界観を更新して、常識test 01とtest 02でそれぞれ自分を実行したのではないでしょうか.なぜ干渉したのですか.
@Scheculedのデフォルトのスレッドプールが単一スレッドであるため、タスクはdelayQueueキューに配置され、キューに並んで実行されます.
だから待機状況が発生し、この問題を解決する方法はデフォルトのスレッドをマルチスレッドに変更することです.
四修正@Scheculedデフォルトスレッドプールがマルチスレッドである
デフォルトのスレッドプールをマルチスレッドに変更します.ここには10個のスレッドがあります.
Spring Bootを再起動すると、test 01は1秒に1回実行され、test 02は1分待つか1分待つか、
2つのタスクの間に待機現象は発生しません.
@EnableScheduling注記を追加する必要があります.
package com.lanhuigu.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* Spring Boot
*
* @author yihonglei
*/
@SpringBootApplication
@EnableScheduling
public class BootApplication {
public static void main(String[] args) {
SpringApplication.run(BootApplication.class, args);
}
}
二@Scheduled注記IDタスク
2つのタスクを作成します.いずれのメソッドも実行後1秒に1回実行され、test 02は1分間停止します.
package com.lanhuigu.springboot.task;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* @Scheduled
*
* @author yihonglei
* @date 2019/1/7 16:54
*/
@Component
public class BootTask {
@Scheduled(fixedDelay = 1000)
public void test01() {
System.out.println("test01 start");
System.out.println(" ...");
System.out.println("test01 end");
}
@Scheduled(fixedDelay = 1000)
public void test02() {
System.out.println("test02 start");
try {
Thread.sleep(60 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("test02 end");
}
}
トリプルタスクテスト
Spring Bootアプリケーションを起動し、コンソール出力を確認します.
test 02を実行すると、test 02が実行されるのを待つため、test 01は1分待ってから実行されます.
世界観を更新して、常識test 01とtest 02でそれぞれ自分を実行したのではないでしょうか.なぜ干渉したのですか.
@Scheculedのデフォルトのスレッドプールが単一スレッドであるため、タスクはdelayQueueキューに配置され、キューに並んで実行されます.
だから待機状況が発生し、この問題を解決する方法はデフォルトのスレッドをマルチスレッドに変更することです.
四修正@Scheculedデフォルトスレッドプールがマルチスレッドである
package com.lanhuigu.springboot.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
/**
* Spring Scheculed
*
* @author yihonglei
* @date 2019/1/7 15:47
*/
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.setScheduler(new ScheduledThreadPoolExecutor(10,
new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "my-schedule-pool");
}
}));
}
}
デフォルトのスレッドプールをマルチスレッドに変更します.ここには10個のスレッドがあります.
Spring Bootを再起動すると、test 01は1秒に1回実行され、test 02は1分待つか1分待つか、
2つのタスクの間に待機現象は発生しません.