Implementation of running status Monitoring of Java Thread Pool
This article introduces the relevant knowledge of "the implementation method of Java thread pool running status monitoring". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In the actual development process, various failures may be encountered in the use of the thread pool, such as thread pool blocking, unable to submit new tasks, and so on.
If you want to monitor the execution status of a thread pool, the thread pool execution class ThreadPoolExecutor also gives the relevant API, which can get the current active threads of the thread pool, the number of threads in the queue, the number of threads that have been executed, the number of buses, and so on.
Number of bus programs = number of queued threads + number of active threads + number of threads completed by execution.
Examples of thread pool usage:
Private static ExecutorService es = new ThreadPoolExecutor (50,100,0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue (100000)); public static void main (String [] args) throws Exception {for (int I = 0; I)
< 100000; i++) { es.execute(() ->{System.out.print (1); try {Thread.sleep (1000);} catch (InterruptedException e) {e.printStackTrace ();}});} ThreadPoolExecutor tpe = ((ThreadPoolExecutor) es); while (true) {System.out.println (); int queueSize = tpe.getQueue (). Size (); System.out.println ("current queue threads:" + queueSize) Int activeCount = tpe.getActiveCount (); System.out.println ("current active threads:" + activeCount); long completedTaskCount = tpe.getCompletedTaskCount (); System.out.println ("execution completed threads:" + completedTaskCount); long taskCount = tpe.getTaskCount (); System.out.println ("Total threads:" + taskCount); Thread.sleep (3000) This is the end of the content of "implementation of Java thread pool running status monitoring". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!