java spring 并行调用_java - Spring @Scheduler并行运行

我有以下3个类(class):

成分Apackage mytest.spring.test.spring;

import org.apache.log4j.Logger;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Component;

@Component

public class ComponentA {

Logger log = Logger.getLogger(ComponentB.class);

@Scheduled(fixedRate=2000)

public void sayHello() {

for(int i=1 ; i<=5 ; i++) {

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

log.info("Hello from ComponentA " + i);

}

}

}

组分B

package mytest.spring.test.spring;

import org.apache.log4j.Logger;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Component;

@Component

public class ComponentB {

Logger log = Logger.getLogger(ComponentB.class);

@Scheduled(fixedRate=2000)

public void sayHello() {

for(int i=1 ; i<=3 ; i++) {

try {

Thread.sleep(2000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

log.info("Hello from ComponentB " + i);

}

}

}

我的应用程序

package mytest.spring.test.spring;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication

@EnableScheduling

public class MyApplication {

public static void main(String[] args) {

SpringApplication.run(MyApplication.class, args);

}

}

当我执行它时,我得到以下输出:

Hello from ComponentA 1

Hello from ComponentA 2

Hello from ComponentA 3

Hello from ComponentA 4

Hello from ComponentA 5

Hello from ComponentB 1

Hello from ComponentB 2

Hello from ComponentB 3

Hello from ComponentA 1

Hello from ComponentA 2

Hello from ComponentA 3

Hello from ComponentA 4

Hello from ComponentA 5

Hello from ComponentB 1

Hello from ComponentB 2

Hello from ComponentB 3

...

我需要2个Scheduled方法并行运行,根据我得到的输出,这显然不是问题。我读到应该可以为@Schedule批注提供一个自定义TaskExecutor,用它可以定义我们想要多少个线程...

我对吗 ?我找不到如何提供此信息。

最佳答案

By default, will be searching for an associated scheduler definition:

either a unique TaskScheduler bean in the context, or a TaskScheduler

bean named "taskScheduler" otherwise; the same lookup will also be

performed for a ScheduledExecutorService bean. If neither of the two

is resolvable, a local single-threaded default scheduler will be

created and used within the registrar.

When more control is desired, a @Configuration class may implement

SchedulingConfigurer. This allows access to the underlying

ScheduledTaskRegistrar instance. For example, the following example

demonstrates how to customize the Executor used to execute scheduled

tasks:

@Configuration

@EnableScheduling

public class AppConfig implements SchedulingConfigurer {

@Override

public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {

taskRegistrar.setScheduler(taskExecutor());

}

@Bean(destroyMethod="shutdown")

public Executor taskExecutor() {

return Executors.newScheduledThreadPool(100);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您想了解关于JavaScheduler以及如何使用它,以下是我为您搜集到的相关信息: SchedulerJava中的一个工具,它用于在指定的时间间隔内执行任务。Java提供了两种类型的Scheduler:Timer和ScheduledExecutorService。 Timer是Java提供的最简单的Scheduler,它允许您安排以固定速率重复执行任务,或者在指定时间内执行任务一次。但是,Timer有一些缺点,例如不能处理多个任务、不能很好地处理异常等。 相比之下,ScheduledExecutorService是Java提供的更高级的Scheduler,它提供了更多的灵活性和控制。ScheduledExecutorService可以安排一个或多个任务在指定时间内执行,也可以安排任务重复执行。此外,它还提供了更好的异常处理和更高的性能。 下面是使用ScheduledExecutorService的示例代码: ``` import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class SchedulerDemo { public static void main(String[] args) { ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); // 安排任务在5秒后执行 scheduler.schedule(new Task(), 5, TimeUnit.SECONDS); // 安排任务在每秒钟执行一次 scheduler.scheduleAtFixedRate(new Task(), 0, 1, TimeUnit.SECONDS); // 关闭scheduler scheduler.shutdown(); } static class Task implements Runnable { @Override public void run() { // 执行任务的代码 } } } ``` 在上面的代码中,我们创建了一个ScheduledExecutorService,然后安排了两个任务。第一个任务会在5秒后执行一次,第二个任务会每秒钟执行一次。最后,我们关闭了scheduler。 希望这些信息能够帮助到您,如果您有任何问题或需要进一步了解,请随时告诉我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值