如何将定时任务分配给特定的线程?

huangapple go评论72阅读模式
英文:

How to assign scheduled tasks to a specific thread?

问题

同事们,我有一组预定的任务。在 Spring Boot 的属性文件中,它看起来像这样:

group1.task1.run = <每隔一分钟执行一次>
group1.task2.run = <每隔两分钟执行一次>

group2.task1.run = <每隔一分钟执行一次>
group2.task2.run = <每隔两分钟执行一次>

是否可能创建两个不同的线程(T1T2),并将第一组预定任务分配到 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 = &lt;execute every first minute&gt;
group1.task2.run = &lt;execute every second minute&gt;


group2.task1.run = &lt;execute every first minute&gt;
group2.task2.run = &lt;execute every second minute&gt;

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 = &quot;threadPool1&quot;)
    public void task1() {
        System.out.println(&quot;Task1: &quot; + Thread.currentThread());
    }

    @Scheduled(fixedRate = 1000)
    @Async(value = &quot;threadPool2&quot;)
    public void task2() {
        System.out.println(&quot;Task2: &quot; + 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(...);

huangapple
  • 本文由 发表于 2020年5月29日 21:24:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/62087041.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定