I’ve written an introduction to the use of Java thread pools called “Thread Pool Overview”. I’ve covered thread pools, thread pool core classes, thread pool workflows, thread pool classification, rejection policies, and how to submit and close thread pools.

However, in the actual development process, in the process of using the thread pool may encounter various aspects of failure, such as thread pool blocking, unable to submit new tasks and so on.

If you want to monitor the execution status of a particular thread pool, the thread pool execution class ThreadPoolExecutor also provides apis to get real-time information about the number of active threads in the thread pool, the number of threads that are queuing, the number of completed threads, the total number of threads, and so on.

Total number of threads = number of queuing threads + number of active threads + number of completed threads.

Below is a thread pool usage example, and teach you to get the thread pool state.

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("Currently active threads:" + activeCount);

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

		long taskCount = tpe.getTaskCount();
		System.out.println("Total threads:"+ taskCount); Thread.sleep(3000); }}Copy the code

The thread pool submitted 100,000 tasks, but only 50 threads were performing work at the same time, so we get the current thread pool running status every 3 seconds.

First program output:

Number of queued threads: 99950 Number of active threads: 50 Number of executed threads: 0 Total number of threads (queued threads + active threads + executed threads) : 100000Copy the code

Second program output:

Number of queued threads: 99800 Number of active threads: 50 Number of completed threads: 150 Total number of threads (queued threads + active threads + completed threads) : 100000Copy the code

The number of active threads and the total number of threads are unchanged. The number of queued threads and the number of completed threads are constantly changing until all tasks are completed.

Number of queued threads: 0 Number of active threads: 0 Number of executed threads: 100000 Total number of threads (queued threads + active threads + executed threads) : 100000Copy the code

Now that you know how these apis are used, you can easily monitor the state of the thread pool if you want.

If you have any questions, leave a message below. If you want to further exchange and learn, you can also click the link to read the original text on the lower left to join the Planet of Java Technology knowledge. At present, knowledge Planet 5.1 Labor Day -5.4 Youth Day discount activities last day, miss today and wait for half a year……

A latest Java technology tutorial, including Java foundation, multithreading, JVM, new features and more actual combat dry goods.