Java countdownlatch completes the detailed explanation of asynchronous callback instance

Java countdownlatch completes the detailed explanation of asynchronous callback instance

Example code:

Here are two points to add:

1. If you are a thread started with the main method, there is no problem calling this method. JDK will ensure that all threads terminate before the main method exits. However, if the main method is not the initiator of the asynchronous task (such as JUnit, spring, Tomcat), the laucher will lose control of the thread once it is started. For example, in JUnit, after the laucher submits the task, it will be considered that all processes have been completed, and other threads will be forcibly terminated.

2. Because of this, please use the correct executor according to the environment. For example, in a web environment, Tomcat should be selected The thread pool managed by (or spring) acts as the executor, so as to ensure that the web application has control over the whole life cycle of asynchronous tasks. What are the consequences if you choose JDK thread pool? The tasks may be executed normally. Once you terminate the web app, the executing asynchronous threads will not be killed normally, resulting in memory leakage or other unforeseen events Consequences.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>