After the Tomcat server is started, there are actually 6 threads working, that is, 1 user threads (Thread[main in the image below), and the remaining 5 are daemon threads (daemon thread in the image below), respectively, through bio.
First daemon thread (Daemon thread [Containerbackgroundprocessor[standardengine[catalina]] (Running)
) is a container spooler thread.
Second daemon thread (Daemon thread [http-bio-8080-acceptor-0] (Running)
) listens on port 8080 and is responsible for establishing an HTTP connection. This connector is used when accessing the Web App for a tomcat server through a browser.
Fifth daemon thread (Daemon thread [ajp-bio-8009-acceptor-0] (Running)
) listens on port 8009 and is responsible for establishing connections to other HTTP servers. This connector is needed to integrate Tomcat with other HTTP servers. Before Nginx appeared, the Web application of static and dynamic separation using APACHE,TOMCAT and Apache communication is the AJP protocol.
Let's take a look at the properties of a thread.
Http-bio-[inetaddress]-[port]-acceptor-number: Default is http-bio-8080-acceptor-0.
1. Priority: Default Priority 5
2. Whether daemon: Daemon threading mode is running
3. Number of Threads: TOMCAT7 boot 1 acceptor threads by default
There is no essential difference between daemon threads and user threads, and they can be switched from one to the other. Converting a user thread to a daemon thread can be accomplished by invoking the Setdaemon (true) method of the Thread object. The difference is the time to exit: User thread, daemon thread->JVM.
A daemon thread is a thread that provides a common service in the background when the program is running, such as a garbage collection thread, an event scheduler thread that handles GUI events, and so on. This thread is not essential, and the program terminates automatically when all the user threads are finished and only the daemon threads are left. Because there is no guardian, the daemon will have no work to do, there is no need to continue to run the program. Conversely, the program will not terminate as long as any user thread is still running. Tomcat's shutdown takes advantage of this principle, as long as the only user thread is closed, the entire application is closed.
The following code verifies that the current user thread has only one, that is, as long as the main thread exits, then the JVM really exits.
Import Java.util.Scanner;
public class Daemonthread implements Runnable {int i = 0;
public void Run () {while (true) {System.out.println (i);
try {thread.sleep (1000);
} catch (Interruptedexception e) {e.printstacktrace ();
} i++; }} @SuppressWarnings ("resource") public static void main (string[] args) {Thread daemonthread = new
Thread (New Daemonthread ());
Set to Daemon Daemonthread.setdaemon (true);
Daemonthread.start ();
System.out.println ("Isdaemon =" + Daemonthread.isdaemon ());
Scanner Scanner = new Scanner (system.in);
Accept the input so that the program pauses and once the user input is accepted, the main thread ends and the JVM exits!
Scanner.next ();
The Addshutdownhook method increases the processing event when the JVM stops: Prints the JVM Exit statement when the JVM exits.
Runtime.getruntime (). Addshutdownhook (New Thread () {@Override public void run () { System.out.println ("JVM EXit! ");}});}