Asynchronous call using Spring and Executor - spring

Do we really required to have Executor for #Async in Spring?
What will happen if i am not using Executor interface on the front of thread pool?

When no custom TaskExecutor is given, or other in other words out-of-box spring uses SimpleAsyncTaskExecutor to create threads to handle async.
SimpleAsyncTaskExecutor -
By default concurrent threads is unlimited. Supports control by concurrencyLimit property
No re-use of threads
TaskExecutor based -
Allows to have thread pool with limited number of threads as required
Since we use thread pools, re-use of threads is done and no overhead.
Javadoc : https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/task/SimpleAsyncTaskExecutor.html

Related

Spring #Scheduled - run concurrently

I have a Spring boot 2.1.6.RELEASE application in which I have a method annotated with
#Scheduled(cron = "*/10 * * * * *}
I want it to run with that cron, EVEN IF another execution is already in progress.
I tried increasing the executor thread number using the application.properties file:
spring.task.scheduling.pool.size=10
But it didn't seem to work as it is still waiting for an execution to finish before starting the next one.
What is the proper way to do parallel executions using a cron in the #Scheduled annotation?
It is true that the default pool size for the task scheduler is 1 but increasing this pool size is only making more threads available for other #Scheduled methods. The intended behaviour is not for methods to run in parallel as otherwise threads could become exhausted.
If you wish to change this behaviour to allow the same method to run in parallel you need to use #EnableAsync and #Async annotations. You might also want to change the pool size of the task executor. That being said, keep in mind that you may still exhaust your threads so be very careful with changing this intended behaviour.

How to know the number of active/alive threads in SyncTaskExecutor?

ThreadPoolTaskExecutor has getActiveCount() method, but SynctaskExecutor has nothing.
How do i know if any thread in running/active/alive in the thread pool in case of SyncTaskExecutor?
There's no thread pool in SyncTaskExecutor. Look at the implementation. This executor doesn't run your task on thread pool. It runs task on calling thread - the thread from which you've called SyncTaskExecutor. So actually it's same as you would run your task directly without SyncTaskExecutor, however it's useful in tests - you can use this implementation and you'll have blocking code, so you won't have to wait for complete of asynchronous invocation.

Issue with Spring Boot Async method

I have a asynchronous method enabled using #Async annotation. At times i am seeing SimpleAsyncTaskExecutor thread count increases exponentially. Any idea on this behavior?
If it increases literally exponentially it sounds like the async method is calling itself perhaps?
By default, Spring uses a SimpleAsyncTaskExecutor to run the methods asynchronously.
SimpleAsyncTaskExecutor spawns a new thread with each task and does not support thread pooling and queueing of tasks.
So, if the async method is called multiple times in a short span of time, multiple threads will be opened for each task
You should define your own executor. Refer the following link
http://www.baeldung.com/spring-async

ThreadPoolTaskExecutor execution strategy

I'm using spring's abstraction ThreadPoolTaskExecutor in order to execute tasks using threads.
The execute method described as (java doc):
Execute the given task.
The call might return immediately if the implementation uses an asynchronous execution strategy, or might block in the case of synchronous execution.
Got 2 questions:
Where can the execution strategy be configured?
If the execution strategy is set to "synchronous", how can it serve me? It seems weird to use an executor that works synchronously.

Multiple Async Http Client Calls in Java is Thread Safe

I am working with Async Http Client in Java with this example . I just want to know about the performance issues when we are calling multiple services at a time asynchronously . I am concerned about the number of CPU cores and the number of threads used in this example. I want to know more about this like :
Is this example uses multiple threads for each request.
If suppose I am making n number of calls at that time CPU cores will not support this to run , how I come to know how many threads it will support based in CPU ?
Is this example is Thread Safe.
Is this example uses multiple threads for each request.
No, it does not
how many threads it will support based in CPU
By default the underlying I/O reactor starts one I/O dispatch thread per CPU core
Is this example is Thread Safe
This question is just too vague. Exactly what class instances are you talking about?
Thread-safety rules that apply to both blocking HttpClient and non-blocking HttpAsyncClient are
clients are thread-safe
connection managers are thread-safe
request / response messages are not thread safe
contexts are not thread safe
As far as HttpAsyncClient is concerned, as long as you do not use additional threads to process / generate content, HttpAsyncClient ensures proper access synchronization of all components involved.

Resources