英文:
How to assign scheduled tasks to a specific thread?
问题
同事们,我有一组预定的任务。在 Spring Boot 的属性文件中,它看起来像这样:
group1.task1.run = <每隔一分钟执行一次>
group1.task2.run = <每隔两分钟执行一次>
group2.task1.run = <每隔一分钟执行一次>
group2.task2.run = <每隔两分钟执行一次>
是否可能创建两个不同的线程(T1
和 T2
),并将第一组预定任务分配到 T1
线程中执行,将第二组任务分配到 T2
线程中执行?
我不能只是增加 TaskScheduler
中的 PoolSize
,因为这样做后,group1
的任务将在不同的线程中执行,这对业务流程不适用。
对于任何建议和帮助,我将不胜感激。
英文:
Collegues, I have a group of tasks that are scheduled. In spring-boot properties file it looks like:
group1.task1.run = <execute every first minute>
group1.task2.run = <execute every second minute>
group2.task1.run = <execute every first minute>
group2.task2.run = <execute every second minute>
Is it possible to create two different threads (T1
and T2
) and assign the first group of scheduled tasks to be executed in T1
thread, and the second group in T2
thread?
I can’t just increase PoolSize
in TaskScheduler
beacuse after that group1
tasks will be execute in different threads, it is not suitable for business process.
I would be grateful for any advice and help.
答案1
得分: 2
据我所知,无法单独指定一个线程。然而,您可以指定应该用于调度任务的Executor
。如果您希望任务在同一个线程中执行,只需创建一个包含单个线程的线程池,如下所示:
@Configuration
@EnableAsync
@EnableScheduling
public class TempConfig {
@Scheduled(fixedRate = 1000)
@Async(value = "threadPool1")
public void task1() {
System.out.println("Task1: " + Thread.currentThread());
}
@Scheduled(fixedRate = 1000)
@Async(value = "threadPool2")
public void task2() {
System.out.println("Task2: " + Thread.currentThread());
}
@Bean
public Executor threadPool1() {
return Executors.newFixedThreadPool(1);
}
@Bean
public Executor threadPool2() {
return Executors.newFixedThreadPool(1);
}
}
当然,您也可以选择自己安排任务:
var s1 = Executors.newScheduledThreadPool(1);
var s2 = Executors.newScheduledThreadPool(1);
// 安排组1的任务
s1.scheduleWithFixedDelay(...);
s1.scheduleWithFixedDelay(...);
// 安排组2的任务
s2.scheduleWithFixedDelay(...);
s2.scheduleWithFixedDelay(...);
英文:
As far as I know it's not possible to specify a single thread. However, you can specify the Executor
which should be used to schedule your tasks. If you wan't your tasks to be executed in the same thread, just create a thread pool with a single thread in it, like so:
@Configuration
@EnableAsync
@EnableScheduling
public class TempConfig {
@Scheduled(fixedRate = 1000)
@Async(value = "threadPool1")
public void task1() {
System.out.println("Task1: " + Thread.currentThread());
}
@Scheduled(fixedRate = 1000)
@Async(value = "threadPool2")
public void task2() {
System.out.println("Task2: " + Thread.currentThread());
}
@Bean
public Executor threadPool1() {
return Executors.newFixedThreadPool(1);
}
@Bean
public Executor threadPool2() {
return Executors.newFixedThreadPool(1);
}
}
Of course, you can alternatively just schedule the tasks yourself:
var s1 = Executors.newScheduledThreadPool(1);
var s2 = Executors.newScheduledThreadPool(1);
// schedule tasks of group1
s1.scheduleWithFixedDelay(...);
s1.scheduleWithFixedDelay(...);
// schedule tasks of group2
s2.scheduleWithFixedDelay(...);
s2.scheduleWithFixedDelay(...);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论