teaches you how to monitor the Java thread pool running state

Source: Internet
Author: User

Prior to writing a Java thread pool usage Introduction article "Full analysis of thread pool", comprehensively describes what is the thread pool, core class of thread pools, thread pooling workflow, cluster classification, deny policy, and how to submit and close the thread pool.

However, in the actual development process, the use of the thread pool may encounter various aspects of the failure, such as the thread pool blocked, unable to submit new tasks.

If you want to monitor the execution status of a thread pool, the thread pooling execution class ThreadPoolExecutor also gives the relevant APIs that can get the current number of active threads in the thread pool, the number of threads in the queue, the number of threads that have been executed, the number of bus routines, and so on.

Bus Path = number of queued threads + Active threads + number of threads executing completed.

Here is an example of a thread pool usage, and teaches you to get the thread pools state.

private static Executorservice es = new Threadpoolexecutor (0L, timeunit.milliseconds, new Linkedblocki Ngqueue<runnable> (100000));p ublic static void Main (string[] args) throws Exception {for (int i = 0; I < 10000 0;            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 number of queued threads:" + queuesize);        int activecount = Tpe.getactivecount ();        System.out.println ("Current number of active threads:" + activecount);        Long Completedtaskcount = Tpe.getcompletedtaskcount ();        System.out.println ("Number of execution Threads:" + completedtaskcount);        Long Taskcount = Tpe.gettaskcount ();        System.out.println ("Number of bus threads:" + taskcount); ThRead.sleep (3000); }}

The thread pool commits 100,000 tasks, but at the same time only 50 threads are executing, and we get the current thread pool's running state every 3 seconds.

First time program output:

当前排队线程数:99950当前活动线程数:50执行完成线程数:0总线程数(排队线程数 + 活动线程数 +  执行完成线程数):100000

Second program output:

当前排队线程数:99800当前活动线程数:50执行完成线程数:150总线程数(排队线程数 + 活动线程数 +  执行完成线程数):100000

The number of active threads and the number of threads is constant, the number of threads in the queue and the number of thread executions are constantly changing until all tasks are completed and the final output:

当前排队线程数:0当前活动线程数:0执行完成线程数:100000总线程数(排队线程数 + 活动线程数 +  执行完成线程数):100000

In this way, you understand how these APIs are used, and it's convenient for you to monitor the status of the thread pool.

Recommended: Spring Boot & Cloud's Strongest technology tutorials

Scan attention to our public number, dry foods updated daily.

teaches you how to monitor the Java thread pool running state

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.