Thread pool description of thread pool


 One, the thread pool

 We know that threads cannot be created indefinitely, but in reality, when concurrent requests for multi-tasking are encountered, multi-threading will be used to process them, but it is impossible to create so many threads. At this time, we will think of thread pools. now,

Speaking of thread pools, it is actually a means of managing multithreading. The code below is the code that describes the usage to be tested,

 

package com.test.thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.junit.Test;

public class TestThreadPool {
	/**
	 * a blocking queue
	*/
	BlockingQueue<Runnable> queues=new LinkedBlockingQueue<Runnable>(10);
	/**
	 * The thread pool initializes 10 threads, the maximum number of threads is 20, and the idle time is 0L seconds
	 */
	ThreadPoolExecutor pool= new ThreadPoolExecutor(10, 20,0L,TimeUnit.MICROSECONDS,queues);
	
	
	
	
	
	/**
	 *
	 * @param threadNum number of execution threads
	 * @throws InterruptedException
	 */
	private void testThreadPool(int threadNum)throws InterruptedException{
		for(int i=1;i<=threadNum;i++){
			pool.execute(new Task("aa@"+i));
		}
		
		//Pause for one second and wait for the initialization threads in the thread pool to start
		Thread.sleep(1000);
		// print how many threads are currently in the blocking queue
		System.out.println("There are many less tasks in the blocking queue at this time: "+" number->"+queues.size()+", specific thread->"+queues);
		System.out.println("Threads executed in the thread pool: "+pool.getActiveCount());
		/**
		 * When the shutdown method is used, the thread pool will no longer receive any new tasks, but the thread pool will not exit immediately at this time, and will not exit until the tasks added to the thread pool have been processed.
		 * After calling the shutdown method, we can use the isTerminated method in an infinite loop to determine whether all the threads in the thread pool have been executed. If the child threads are finished, we can do subsequent operations such as closing the stream.
		 */
		pool.shutdown();
		while(true){
			if(pool.isTerminated()){
				System.out.println("All tasks completed...");
				break;
			}
			Thread.sleep(200);
		}
	}
	
	
	/**
	 * Test 5 threads
	 * @throws InterruptedException
	 */
	@Test
	public void testThreadPool5() throws InterruptedException {
		testThreadPool(5);
	}

	
	/**
	 * Test 15 threads
	 * @throws InterruptedException
	 */
	@Test
	public void testThreadPool15() throws InterruptedException{
		testThreadPool(15);
	}

	/**
	 * Test 25 threads
	 * @throws InterruptedException
	 */
	@Test
	public void testThreadPool25() throws InterruptedException{
		testThreadPool(25);
	}
	
	/**
	 * Test 45 threads
	 * @throws InterruptedException
	 */
	@Test
	public void testThreadPool45() throws InterruptedException{
		testThreadPool(45);
	}

	
}

class Task implements Runnable{
	String name;
	public Task(String name) {
	  this.name=name;
	}
	@Override
	public void run() {
		System.out.println("Thread["+name+"] start... start executing task");
		try {
			Thread.sleep(30000L);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
		System.out.println("Thread["+name+"] ends---complete task");
	}
	@Override
	public String toString() {
		return name;
	}
}

 

 (1) When the testThreadPool5() method is executed, the number of threads here is 5 

   which prints the result

 

Thread [aa@2] started... start executing task
Thread [aa@1] started... start executing task
Thread [aa@4] started... start executing task
Thread [aa@3] started... start executing task
Thread [aa@5] started... start executing task
At this time, the blocking queue has multiple tasks: number -> 0, specific thread -> []
Threads executed in thread pool: 5
Thread [aa@1] ends --- completes the task
Thread [aa@5] ends --- completes the task
Thread [aa@3] ends --- completes the task
Thread [aa@4] ends --- completes the task
Thread [aa@2] ends --- completes the task
All tasks completed...  

  From the print results, we can see that  since the given number of execution threads 5 is less than the initialization number 10, no extra threads will be put into the blocking queue

 

 

(2) When the testThreadPool15() method is executed, the thread to be executed here is 15

 which prints the result

 

Thread [aa@1] started... start executing task
Thread [aa@3] started... start executing task
Thread [aa@2] started... start executing task
Thread [aa@4] started... start executing task
Thread [aa@5] started... start executing task
Thread [aa@6] started... start executing task
Thread [aa@7] started... start executing task
Thread [aa@9] started... start executing task
Thread [aa@8] started... start executing task
Thread [aa@10] started... start executing task
At this time, the blocking queue has multiple tasks: number->5, specific thread->[aa@11, aa@12, aa@13, aa@14, aa@15]
Threads executed in the thread pool: 10
Thread [aa@2] ends --- completes the task
Thread [aa@1] ends --- completes the task
Thread [aa@3] ends --- completes the task
Thread [aa@11] started... start executing task
Thread [aa@6] ends --- completes the task
Thread [aa@5] ends --- completes the task
Thread [aa@15] started... start executing task
Thread [aa@4] ends --- completes the task
Thread [aa@12] started... start executing task
Thread [aa@13] started... start executing task
Thread [aa@14] started... start executing task
Thread [aa@10] ends --- completes the task
Thread [aa@7] ends --- completes the task
Thread [aa@9] ends --- completes the task
Thread [aa@8] ends --- completes the task
Thread [aa@11] ends --- completes the task
Thread [aa@15] ends --- completes the task
Thread [aa@12] ends --- completes the task
Thread [aa@13] ends --- completes the task
Thread [aa@14] ends --- completes the task
All tasks completed...  

   From the print results, we can see that   because the given number of execution threads 15 is greater than the initialization number 10 and less than the maximum number of threads 20, the excess threads are put into the blocking queue as 5, and when the first ten threads are gradually executed, blocking The threads in the queue also start to join the execution, and finally the queue is all completed

 

(3) When the testThreadPool25() method is executed, the given execution thread is 25

which prints the result

Thread [aa@1] started... start executing task
Thread [aa@4] started... start executing task
Thread [aa@3] started... start executing task
Thread [aa@5] started... start executing task
Thread [aa@6] started... start executing task
Thread [aa@2] started... start executing task
Thread [aa@7] started... start executing task
Thread [aa@8] started... start executing task
Thread [aa@9] started... start executing task
Thread [aa@10] started... start executing task
Thread [aa@21] started... start executing task
Thread [aa@23] started... start executing task
Thread [aa@22] started... start executing task
Thread [aa@24] started... start executing task
Thread [aa@25] started... start executing task
At this time, the blocking queue has multiple tasks: number->10, specific thread->[aa@11, aa@12, aa@13, aa@14, aa@15, aa@16, aa@17, aa@ 18, aa@19, aa@20]
Threads executed in thread pool: 15
Thread [aa@10] ends --- completes the task
Thread [aa@2] ends --- completes the task
Thread [aa@7] ends --- completes the task
Thread [aa@5] ends --- completes the task
Thread [aa@8] ends --- completes the task
Thread [aa@4] ends --- completes the task
Thread [aa@9] ends --- completes the task
Thread [aa@16] started... start executing task
Thread [aa@1] ends --- completes the task
Thread [aa@14] started... start executing task
Thread [aa@15] started... start executing task
Thread [aa@13] started... start executing task
Thread [aa@3] ends --- completes the task
Thread [aa@19] started... start executing task
Thread [aa@12] started... start executing task
Thread [aa@11] started... start executing task
Thread [aa@6] ends --- completes the task
Thread [aa@20] started... start executing task
Thread [aa@18] started... start executing task
Thread [aa@17] started... start executing task
Thread [aa@23] ends --- completes the task
Thread [aa@21] ends --- completes the task
Thread [aa@22] ends --- completes the task
Thread [aa@24] ends --- completes the task
Thread [aa@25] ends --- completes the task
Thread [aa@16] ends --- completes the task
Thread [aa@17] ends --- completes the task
Thread [aa@18] ends --- completes the task
Thread [aa@20] ends --- completes the task
Thread [aa@11] ends --- completes the task
Thread [aa@12] ends --- completes the task
Thread [aa@19] ends --- completes the task
Thread [aa@13] ends --- completes the task
Thread [aa@15] ends --- completes the task
Thread [aa@14] ends --- completes the task
All tasks completed...  

 From the print results, we can see that   since the given number of execution threads 25 is greater than the initialization number 10 and greater than the maximum number of threads 20, the excess threads put into the blocking queue are 10, which means that there are 15 threads executing in the thread pool. (This quantity is less than the maximum quantity limit of 20)

 

(3) When the testThreadPool45() method is executed, the given execution thread here is 45

  At this time, the printing result will report an error as shown below

 

 
Since the given number of execution threads 25 is greater than the initialization number 10 is greater than the maximum number of threads 20, and the blocking queue can be placed at most 10, there are 25 threads in the thread pool to execute, but because the maximum number of thread pools is 20, 25 cannot be executed at the same time So report an error

 

To sum up, assume the number of execution threads threadNum, the number of initialized threads  corePoolSize, the maximum allowable number of threads to execute maximumPoolSize, and the blocking queue capacity queueNums

 

1. When threadNum<corePoolSize is executed directly, there are no extra threads in the blocking queue to put

2. When corePoolSize<threadNum<maximumPoolSize, the number of blocked queue threads min(queueNums,threadNum-corePoolSize) executes the number of threads corePoolSize 

3. When threadNum>maximumPoolSize and (threadNum-min(queueNums,threadNum-corePoolSize))<maximumPoolSize the number of blocked queue threads min(queueNums,threadNum-corePoolSize), the number of threads executed is threadNum-min(queueNums,threadNum-corePoolSize)

4. When 3, when threadNum>maximumPoolSize and (threadNum-min(queueNums,threadNum-corePoolSize))>maximumPoolSize, an error will be reported directly and an exception will be thrown

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326943179&siteId=291194637