Teach you how to monitor the running status of the Java thread pool

Reprinted from  dry goods | Teach you how to monitor the running status of the Java thread pool

I wrote an introduction to the use of Java thread pools before, " Comprehensive Analysis of Thread Pools ", which comprehensively introduces what thread pools are, thread pool core classes, thread pool workflow, thread pool classification, rejection strategy, and how to submit and close threads pool etc.

However, in the actual development process, various faults may be encountered during the use of the thread pool, such as thread pool blocking and inability to submit new tasks.

If you want to monitor the execution status of a thread pool, the thread pool execution class also provides related APIs, which can obtain the current number of active threads in the thread pool, the number of threads in queue, the number of threads that have been executed, the total number of threads in the thread pool in real time number of threads, etc. ThreadPoolExecutor

Total Threads = Queued Threads + Active Threads + Execution Completed Threads.

The following gives an example of thread pool usage and teaches you to get the thread pool status.

private static ExecutorService es = new ThreadPoolExecutor(50, 100, 0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(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 number of queued threads: " + queueSize);

        int activeCount = tpe.getActiveCount();
        System.out.println("Number of active threads: " + activeCount);

        long completedTaskCount = tpe.getCompletedTaskCount();
        System.out.println("Number of completed threads: " + completedTaskCount);

        long taskCount = tpe.getTaskCount();
        System.out.println("Total number of threads: " + taskCount);

        Thread.sleep(3000);
    }

}

The thread pool has submitted 100,000 tasks, but only 50 threads are performing work at the same time. We get the running status of the current thread pool every 3 seconds.

The first program output:

Current number of queued threads:
99950

Current number of active threads:
50

Number of execution completion threads:
0

Total number of threads (number of queued threads + number of active threads + number of execution completion threads):
100000

The second program output:

Current number of queued threads:
99800

Current number of active threads:
50

Number of execution completion threads:
150

Total number of threads (number of queued threads + number of active threads + number of execution completion threads):
100000

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

Current number of queued threads:
0

Current number of active threads:
0

Number of execution completion threads:
100000

Total number of threads (number of queued threads + number of active threads + number of execution completion threads):
100000

In this way, you know how to use these APIs, and it is very convenient if you want to monitor the status of the thread pool.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325536275&siteId=291194637