Spring asyncexecutor FeedbackIntroductory
Originally wanted to see spring on the main content of Async&sync Taskexecutor, looking at the discovery Listenabletaskexecutor is also very interesting, Futuretask is also very interesting, and finally found that This code design is really interesting.
Simpleasynctaskexecutorstructure Diagram
To understand Simpleasynctaskexecutor, let's take a look at its class diagram and inheritance relationship.
What you want to say here is that every time a user submits a new task to simpleasynctaskexecutor, it starts a new thread to respond to the customer request and automatically destroys it after processing the customer request, which means that it does not provide the thread pool functionality, such as
If you want to implement the function of the thread pool, you can implement the Customizablethreadcreator to get the necessary function, this is not the point, the focus is to find its monitoring future API, suddenly want to find the principle:
Listenablefuturetask
Each listenablefuturetask is both a task and a future, the key is that it aggregates a strategy: listenablefuturecallbackregistry
Listenablefuturetask to callback Listenablefuturecallbackregistry's Onsuccess or OnFailure method by overriding the done method of Futuretask
Futuretask
Futuretask Template The Done method, left to the successor to implement, very flexible, where the JDK source code The queueingfuture is aggregated in the executorcompletionservice :
Executorcompletionservice can asynchronously handle tasks that do not need to be ordered, and ultimately get task execution results from a queue.
Flowchart of Realization Idea
Personally feel that the above figure and the notes in the picture are very clear.
From Simpleaynctaskexecutor to Listenablefuturetask