I plan to learn more about Java recently and create some demos.
Knowledge points to know:
- The stack of data requests, because the database lock table has been encountered, it needs to be located. (Take some records today, but it's a bit imperfect)
- I want to get the thread stack that the database connection has not been released.
- Find a sorting tool class for access records.
Thread pool information acquisition
- Thread pool information acquisition or alarm can be useddynamic-tp
- You can use the following code to obtain thread information of tomcat
//Get the webServer thread poolThreadPoolExecutor executor = (ThreadPoolExecutor) ((TomcatWebServer) ()) .getTomcat() .getConnector() .getProtocolHandler() .getExecutor(); Map<String, String> returnMap = new LinkedHashMap<String, String>(); ("Number of core threads", (())); ("Maximum number of threads", (())); ("Number of active threads", (())); ("Current number of threads in the pool", (())); ("Maximum number of threads in history", (())); ("Thread allows idle time/s", (())); ("Is the number of core threads allowed to be recycled?", (())); ("Total number of tasks submitted", (())); ("Total number of historical tasks executed(approximation)", (())); ("Total number of tasks completed in history(approximation)", (())); ("Number of Work Queue Tasks", (().size())); ("Rejection Policy", ().getClass().getSimpleName()); (returnMap);
Get the thread stack information of tomcat
First, you need to understand the status of the thread:
(Create) Creation state: When a thread that has been created is not started, that is, it is in this state before the start method has been called.
(Runtime) Running state: When the thread has been occupied and is executed normally in the Java virtual machine, it is in this state.
(When queued) Blocked state: When a thread tries to acquire an object lock and the object lock is held by other threads, the thread enters the Blocked state. When the thread holds a lock, the thread will automatically become RUNNABLE state.
(Sleep) Sleep state: When a thread is waiting for another thread to perform a (wake-up) action, the thread enters the Waiting state. It cannot automatically wake up after entering this state. You must wait for another thread to call the notify or notifyAll method before it can wake up.
5. TIMED_WAITING (Specify Sleep Time) Specify the time sleep state: basically the same as the WAITING state, with an additional timeout parameter. When the corresponding method is called, the thread will enter the TIMED_WAITING state. This state will remain until the timeout is over or the wake-up notification is received. Common methods with timeout parameters include lock object.wait().
(End) End state: Death due to normal exit from the RUNNABLE state, or death due to an uncatched exception that terminates the RUNNABLE state.
What are the threads that the system starts
count:18 method2:Thread pool threads:Thread[Catalina-utility-1,1,main] method2:Thread pool threads:Thread[Catalina-utility-2,1,main] method2:Thread pool threads:Thread[container-0,5,main] method2:Thread pool threads:Thread[File Watcher,5,main] method2:Thread pool threads:Thread[Live Reload Server,5,main] -----------------The name containshttp Default initial10Threads -----------------AcceptorThreads are mainly used to listen to sockets,Forward the connected socket toPollerThread。 -----------------PollerThread主要用于以较少的资源轮询已连接套接字以保持连接,当数据可用时转给工作Thread。 method2:Thread pool threads:Thread[http-nio-0.0.0.0-8089-exec-1,5,main] method2:Thread pool threads:Thread[http-nio-0.0.0.0-8089-exec-2,5,main] method2:Thread pool threads:Thread[http-nio-0.0.0.0-8089-exec-3,5,main] method2:Thread pool threads:Thread[http-nio-0.0.0.0-8089-exec-4,5,main] method2:Thread pool threads:Thread[http-nio-0.0.0.0-8089-exec-5,5,main] method2:Thread pool threads:Thread[http-nio-0.0.0.0-8089-exec-6,5,main] method2:Thread pool threads:Thread[http-nio-0.0.0.0-8089-exec-7,5,main] method2:Thread pool threads:Thread[http-nio-0.0.0.0-8089-exec-8,5,main] method2:Thread pool threads:Thread[http-nio-0.0.0.0-8089-exec-9,5,main] method2:Thread pool threads:Thread[http-nio-0.0.0.0-8089-exec-10,5,main] method2:Thread pool threads:Thread[http-nio-0.0.0.0-8089-Poller,5,main] method2:Thread pool threads:Thread[http-nio-0.0.0.0-8089-Acceptor,5,main] -------------------- method2:Thread pool threads:Thread[DestroyJavaVM,5,main]
Code
Thread mainThread = (); ThreadGroup mainThreadThreadGroup = (); //Get the threads in the thread group.int count = (); ("count:"+count); Thread[] threads = new Thread[count]; //enumerate enumeration, recurse(threads, true); (threads).filter(Thread::isAlive).forEach(thread -> ("Method 2: Threads of thread pool:" + thread ));
The http thread is based on the above information
Exclusion of WAITING state, filter the name, remove the current thread, because I don’t know how to get the thread from the thread pool obtained from the above, I get all the filters here.
Map<Thread, StackTraceElement[]> allThread = (); for (Thread t : ()) { /** * A thread can be in a state at a given point in time. These states are virtual machine states that do not reflect any operating system thread state. * * Thread state. A thread can be in one of the following states: * NEW Threads that have not been started are in this state. * RUNNABLE The thread executing in the Java virtual machine is in this state. * BLOCKED The thread blocked waiting for the monitor to lock is in this state. * WAITING The thread waiting for another thread to perform a specific action is in this state. * TIMED_WAITING The thread waiting for another thread to execute an action to reach the specified waiting time is in this state. * TERMINATED The exited thread is in this state. * */ StringBuilder sb=new StringBuilder(); if(!(()) && ().indexOf("http")>-1 && !().equals(t)) { (()+":"+()); (0); for (StackTraceElement ele : ()) { (()).append(".").append(()).append(".").append(()).append("$").append(()).append("\n"); } (()); } }
test
@RequestMapping(value = "/hello") public String testHello(Model model) throws InterruptedException { (5000); ("currentTime", new Date()); return "hello"; } @RequestMapping(value = "/hello2") public String testHello2(Model model) throws InterruptedException { for(int i=0;i<Integer.MAX_VALUE;i++){ for(int j=0;j<Integer.MAX_VALUE;j++){} } ("currentTime", new Date()); return "hello"; }
The above is a detailed explanation of the method of tomcat to obtain execution thread pool information and thread stack. For more information about tomcat thread pool information and thread stack, please pay attention to my other related articles!