简体   繁体   English

Spring Boot Batch:CPU性能优化示例

[英]Spring Boot Batch: CPU Performance Optimized Example

In the answer for Configuring Spring Batch Steps in Parallel (Split) using Annotations an example ( FlowJobBuilderTests.java ) is given. 使用注释以并行方式(拆分)配置Spring Batch步骤的答案中,给出了一个示例( FlowJobBuilderTests.java )。

Question: is this answer really suitable for performance optimization? 问题:这个答案真的适合性能优化吗? (I have some long running CPU consuming tasks to perform and need to use the available CPUs in an optimized way) (我需要执行一些耗时长的CPU任务,并且需要以优化的方式使用可用的CPU)

Example you are pointing to uses SimpleAsyncTaskExecutor . 您所指向的示例使用SimpleAsyncTaskExecutor

Javadocs: Javadocs:

NOTE: This implementation does not reuse threads! 注意:此实现不会重用线程! Consider a thread-pooling TaskExecutor implementation instead, in particular for executing a large number of short-lived tasks. 请考虑使用线程池TaskExecutor实现,特别是对于执行大量短期任务而言。

So in order to make your CPU utilization more efficient, I would rather use ThreadPoolTaskExecutor or ConcurrentTaskExecutor . 因此,为了使您的CPU使用效率更高,我宁愿使用ThreadPoolTask​​Executor或ConcurrentTaskExecutor

As your tasks are CPU bound, I would configure thread pool size to amount of CPU cores on machine it will be running. 由于您的任务受CPU约束,因此我将线程池的大小配置为它将运行的计算机上的CPU核心数量。 So that your CPU cycles wouldn't be wasted on switching threads. 这样您的CPU周期就不会浪费在切换线程上。

(Having bigger thread pool size would be suitable for IO bounds tasks, so that CPU wouldn't be waiting for IO operations) (具有更大的线程池大小将适合IO边界任务,因此CPU不会等待IO操作)

Reaction on comment: 对评论的反应:

Now I better understand your concerns. 现在,我更好地了解了您的担忧。 Yes you can run steps in parallel. 是的,您可以并行运行步骤。 Here is example from my book about enterprise Spring : 这是我关于企业Spring的书中的示例

  @Bean
  public Job prepareTeaJob(JobBuilderFactory jobBuilderFactory,
      @Qualifier("boilWaterStep") Step boilWaterStep,
      @Qualifier("addTeaStep") Step addTeaStep,
      @Qualifier("addSugarStep") Step addSugarStep,
      @Qualifier("addWaterStep") Step addWaterStep,
      TaskExecutor customTaskExecutor) {
    Job job = jobBuilderFactory.get("prepareTeaJob")
        .start(boilWaterStep)
        .split(customTaskExecutor)
        .add(new FlowBuilder<Flow>("addIngredientsSplit")
            .from(boilWaterStep).next(addTeaStep)
            .from(boilWaterStep).next(addSugarStep)
            .end())
        .next(addWaterStep)
        .end()
        .build();
    return job;
  }
}

From this example addTeaStep and addSugarStep will be executed in parallel, right after boilWaterStep . 在此示例中, addSugarStepboilWaterStep之后,将并行执行addTeaStepboilWaterStep

Using 使用

        Thread thread = Thread.currentThread();
        System.out.println(thread);

in the steps shows that the example really uses threads. 在步骤中显示,该示例确实使用了线程。 Therefore the example is suitable. 因此,该示例是合适的。

Another good example that uses various threads is: 使用各种线程的另一个好示例是:

https://examples.javacodegeeks.com/enterprise-java/spring/batch/spring-batch-multithreading-example/ by Ashraf Sarhan. 由Ashraf Sarhan提供的https://examples.javacodegeeks.com/enterprise-java/spring/batch/spring-batch-multithreading-example/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM