Brief introduction:

JUC concurrent programming package was added in Jdk1.5, which greatly simplifies the traditional multi-threaded development. As a Java developer, it is necessary to understand the use of thread pool to manage threads in concurrent scenarios.

Concept:

Java thread pool is the product of typical pooling ideas, similar to database connection pool, Redis connection pool, etc. The pooling of ideas, is at the initial time to apply for resources, create a batch of links can be used, in the heart of the process tasks into a queue, and then start the task after the thread creation, if the number of threads exceeded the maximum number exceeds the number of threads waiting in line, other threads, and then removed from the queue tasks to perform;

The thread pool rejects the fulfillment condition:

In the JAVA thread pool, there are three important parameters that determine whether the rejection policy is used:

1. CorePoolSize – Number of core threads, which is the minimum number of threads.

2. WorkQueue – Specifies a blocking queue.

3. MaximumPoolSize – Maximum number of threads

If the number of submitted tasks is greater than corePoolSize, the tasks are placed in the workQueue blocking queue first. When the blocking queue is saturated, the number of threads in the thread pool is expanded until maximumPoolSize is reached. At this point, additional tasks trigger the thread pool rejection policy.

Specific types and definitions of thread pools:

RejectedExecutionHandler provides a top-level interface. The rejectedExecution method is to customize the execution logic of a specific rejection policy.

The Jdk provides four rejection policies by default:

1.CallerRunsPolicy – When the reject policy is triggered, run the task directly using the calling thread as long as the thread pool is not closed. Generally, concurrency is small and does not require high performance. Failure is not allowed. However, because the caller runs the task by himself, if the task is submitted too fast, the program may be blocked, resulting in a large loss of performance and efficiency

2. AbortPolicy – discard task, and throw refuse to enforce RejectedExecutionException exception information. Default reject policy for the thread pool. We must handle the exceptions thrown by the calling thread, otherwise the current execution process will be interrupted and the subsequent task execution will be affected.

3.DiscardPolicy – Discards cards without performing other operations

DiscardOldestPolicy – Discards the oldest task in the blocking workQueue and adds a new task to the queue if the thread pool is not closed

Rejection policy rewrite:

In the actual development, the four rejection policies commonly used by thread pools may not be able to meet the needs of our business scenarios, so the rejection policies need to be rewritten.

For Example:

1. Reject the interface implementation of the policy

public interface RejectedExecutionHandler {  
void rejectedExecution(Runnable r, 
    ThreadPoolExecutor executor);
 }Copy the code

2. Rewrite example:

We just need to pass our custom reject policy for the thread pool when the thread pool is created to change the original reject policy;

The Demo is as follows:

package com.hinson.study;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
public class RejectedDemo implements RejectedExecutionHandler{
	public RejectedDemo(){	}
	
	@Override
	public void rejectedExecution(Runnable runnable , ThreadPoolExecutor executor) {		System.out.println("Thread pool custom handling..");
		System.out.println("The current rejected task is:"+ runnable.toString()); }}Copy the code