ThreadPoolExecutor four kinds of denial strategies (TODO unfinished)

ThreadPoolExecutor four kinds of denial strategy

Problem Scenario


When the added amount exceeds a certain number of tasks (maximumPoolSize + BlockingQueue queue length), by default it will throw an exception. This situation will cause some acquisition, conversion error, omission, does not meet expectations.

The desired result is that pagination loop through basic data collection and conversion tasks to create each piece of data is sequentially added to the task queue, the task queue is full speed when the main thread is blocked waiting for, or mitigate create and join.

Strategy concept


1. Strategy 1: ThreadPoolExecutor.AbortPolicy (), suspend the policy, we will throw RejectedExecutionException. (Default)

When the task is no longer submitted, an exception is thrown, timely feedback program running.

Notes:In the default ThreadPoolExecutor.AbortPolicy, the handler throws a runtime RejectedExecutionException upon rejection

Be used: when the system can not carry a greater amount of concurrency, by timely abnormal findings.

2. 策略2:ThreadPoolExecutor.CallerRunsPolicy()

CallerRunsPolicy literal translation is the caller's execution policy.

Notes:A handler for rejected tasks that runs the rejected task directly in the calling thread of the execute method, unless the executor has been shut down, in which case the task is discarded.

Usage scenarios: business is not very critical (high fault tolerance), recommend this denial policy

3.策略3:ThreadPoolExecutor.DiscardOldestPolicy()

FIFO (to join the ranks of the oldest) task policy.

Notes:In ThreadPoolExecutor.DiscardOldestPolicy, if the executor is not shut down, the task at the head of the work queue is dropped, and then execution is retried (which can fail again, causing this to be repeated.

Usage scenarios: data acquisition (to allow time to abandon the longest task)

4. 策略4:ThreadPoolExecutor.DiscardPolicy()

Notes:In ThreadPoolExecutor.DiscardPolicy, a task that cannot be executed is simply dropped.

Usage scenarios: the task queue is full not throw an exception (which is how much heart, ah, they did not know why not design a way to throw an exception)

Policy is applied 2 - write small programs to achieve a similar kind of everything in FileScanner


package task;

import java.io.File;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

@SuppressWarnings("ALL")
public class FileScanner {
    //1.核心线程数:始终运行的线程数量
    //2.最大线程数:有新任务,并且当前运行线程数小于最大线程数,会创建新的线程来处理任务(正式+临时)
    //3-4.超过3这个数量,4这个时间单位,2-1(最大线程数-核心线程数)这些线程(临时)就会关闭
    //5.工作的阻塞队列
    //6.如果超出工作队列的常务,任务要处理的方式(4种策略)
    private static volatile AtomicInteger count= new AtomicInteger();

    private Object lock=new Object();

    private Semaphore semaphore=new Semaphore(0);//acquire()阻塞等待一定数量的许可

    private ThreadPoolExecutor pool=new ThreadPoolExecutor(
            3,3,0, TimeUnit.MICROSECONDS,
            new LinkedBlockingQueue<>(),new ThreadPoolExecutor.CallerRunsPolicy());

    private ScanCallback callback;

    public FileScanner(ScanCallback callback) {
        this.callback=callback;
    }

//多线程的任务等待
//最开始,不知道有多少子文件夹,不知道应该启动多少个线程
    public void scan(String path) {
       //递归使用线程池
        count.incrementAndGet();
        doScan(new File(path));
    }
    private void doScan(File dir){
        callback.callback(dir);
        pool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    File[] children = dir.listFiles();//下一级文件和文件夹
                    if (children != null) {
                        for (File child : children) {
                            if (child.isDirectory()) {
                                count.incrementAndGet();//启动子目录文件夹
                                doScan(child);
                            }
                        }
                    }
                }
                finally{
                    int r = count.decrementAndGet();
                    if (r == 0) {
                        semaphore.release();
                    }
                }
            }
        });
    }

    public void waitFinish() throws InterruptedException {
        semaphore.acquire();
    }
   

}

 

Published 112 original articles · won praise 171 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43914278/article/details/104321675