In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how many threads are created when starting a simplest Java main program". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it. Study and learn together "how many threads are created when starting a simplest Java main program"!
In java, starting a simple main program does not just create a main thread, the JVM will automatically create some auxiliary threads, these 4 threads, plus, main, so there will be a total of 5 threads created mainly as follows:
Attach Listener: The Attach Listener thread is responsible for receiving external commands, executing them and returning the results to the sender. Usually we will use some commands to ask jvm to give us some feedback information, such as java -version, jmap, jstack, etc. If the thread is not initialized when jvm starts, it will start the first time the user executes the jvm command.
Signal Dispatcher: The first Attach Listener thread mentioned above is responsible for receiving external jvm commands. When the command is successfully received, it will be handed over to the signal dispatcher thread to distribute the commands to different modules and return the processing results. The signal dispater thread also initializes when it receives an external jvm command for the first time.
Finalizer: This thread is also created after the main thread, its priority is 10, mainly used to call the finalize() method of the object before garbage collection;
Reference Handler: VM creates Reference Handler thread after creating main thread, with the highest priority of 10, which is mainly used to handle garbage collection problems of reference object itself (soft reference, weak reference, virtual reference).
The above part of the content is quoted from http://ifeve.com/jvm-thread/, you can take this address to view more thread information
These four threads, plus main, so there's a total of five threads created, and you can see that in these lines of code.
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false,false);
for (ThreadInfo info : threadInfos ) {
System.out.println("[" + info.getThreadId() + "]" + info.getThreadName());
}
However, there is only one thread currently active, which is main, System.out.println(Thread.activeCount()); the return value with this command is 1. This is also often used as a judgment in the following situations
For example, after creating multiple child threads in the main thread, if the child threads take longer than the main thread, the main thread will end before the child threads. If the result of the main thread needs to use the return value of the child thread, then we usually add this code to the main thread:
System.out.println(Thread.activeCount() >1),
To determine whether all child threads run to the end, that is, the current active thread only one main thread. That's right under Windows, but!!
This is not the case under linux, you will find that even if all the child threads are finished, the current active thread is 2!!! Instead of 1, in addition to the main thread, there will be a Monitor Ctrl-Break thread under Linux. What is this thread used for?
Monitoring Thread Activity With Thread Dumps Thread dumps, or "thread stack traces," reveal information about an application's activity that can help you diagnose problems and better optimize application and JVM performance; for example, thread dumps can show the occurrence of "deadlock" conditions, which can seriously impact application performance. You can create a thread dump by invoking a control break (usually by pressing Ctrl-Break or Ctrl- or SIGQUIT on linux). This section provides information on working with thread dumps. It includes information on these subjects: 1.Lock Information in Thread Dumps 2.Detecting Deadlocks
This is Oracle's official website. Everyone can translate themselves. Conclusion: Thread.activeCount() >1 can be used in windows to determine whether all child threads have ended, but Thread.activeCount() > 2 should be used in linux.
Thank you for reading, the above is "how many threads are created when starting a simplest Java main program" content, after learning this article, I believe we have a deeper understanding of how many threads are created when starting a simplest Java main program, and the specific use needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.