0
Follow
0
View

Java multithreading asynchronous related issues.

zcju001 注册会员
2023-02-27 23:50
< div class = "md_content_show e397 data - v - 3967" = "" >

has set up a custom thread pool < br / >

< div class="aw-list-img"> img


But one thread is working at a time.

csu123 注册会员
2023-02-27 23:50

Async does not take effect. Cause:
Two methods are in the same class, but one method calls another asynchronous method.

solution: Split the two methods, put the asynchronous method in a separate class, and then call the

solution
a215555 注册会员
2023-02-27 23:50

Each time a thread is used, it is working.

cupidcool 注册会员
2023-02-27 23:50

for reference only: The
code uses the @Async annotation to mark the method as asynchronous, but by default Spring asynchronous execution is implemented based on a thread pool with a default size of 1. Therefore, this code might only process one element at a time, rather than multiple elements at the same time.

To process multiple elements simultaneously, you can increase the capacity for concurrent processing by configuring the size of the thread pool. You can add the following to the configuration class to set the size of the thread pool to 10:

@Configuration
@EnableAsync
public class AppConfig implements AsyncConfigurer {
 
    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(500);
        executor.setThreadNamePrefix("MyExecutor-");
        executor.initialize();
        return executor;
    }
}

setCorePoolSize and setMaxPoolSize set the number of core threads and maximum number of threads in the thread pool, setQueueCapacity sets the capacity of the thread pool queue, and setThreadNamePrefix sets the prefix of the thread name. The initialize method initializes the thread pool. In this way, multiple elements can be processed simultaneously.

At the same time, it is important to note that increasing the thread pool size may occupy more system resources. Therefore, you need to set the thread pool size appropriately based on the actual situation.

About the Author

Question Info

Publish Time
2023-02-27 23:50
Update Time
2023-02-27 23:50

Related Question

新的NetSuite UI InlineHTML元素在Javascript中不可用

我是否应该关闭谷歌云Pub子发布者后,每次发布在Java

Java编程求体积表面积

vue+java请问一下有什么可以实现这种文件夹层级结构页面的技术和方法

如何在Java中传递列表作为参数的值

关于#Java#的问题,如何解决格式转换?

怎么把eclipse中写好javaweb的项目用idea打开

JAVA 包GoRobotGrpc

Java字符串数组与

javal摄氏度华氏度转换