SimpleThreadPool给线程池增加自动扩充线程数量,以及闲时自动回收的功能

Posted zheaven

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SimpleThreadPool给线程池增加自动扩充线程数量,以及闲时自动回收的功能相关的知识,希望对你有一定的参考价值。

给线程池增加自动扩充线程数量,以及闲时自动回收的功能

package com.dwz.concurrency.chapter13;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class SimpleThreadPool4 extends Thread {
    private static int size;
    private final int queueSize;
    private final static int DEFAULT_TASK_QUEUE_SIZE = 2000;
    private static volatile int seq = 0;
    private final static String THREAD_PREFIX = "SIMPLE_THREAD_POOL-";
    private final static ThreadGroup GROUP = new ThreadGroup("Pool_Group");
    private final static LinkedList<Runnable> TASK_QUEUE = new LinkedList<>();
    private final static List<WorkerTask> THREAD_QUEUE = new ArrayList<>();
    private final DiscardPolicy discardPolicy;
    private final static DiscardPolicy DEFAULT_DISCARD_POLICY = () -> {
        throw new DiscardException("Discard this task.");
    };
    private volatile boolean destroy = false;
    private int min;
    private int max;
    private int active;

    public SimpleThreadPool4() {
        this(4, 8, 12, DEFAULT_TASK_QUEUE_SIZE, DEFAULT_DISCARD_POLICY);
    }

    public SimpleThreadPool4(int min, int active, int max, int queueSize, DiscardPolicy discardPolicy) {
        this.min = min;
        this.active = active;
        this.max = max;
        this.queueSize = queueSize;
        this.discardPolicy = discardPolicy;
        init();
    }

    private void init() {
        for (int i = 0; i < this.min; i++) {
            createWorkTask();
        }
        this.size = min;
        this.start();
    }

    public void submit(Runnable runnable) {
        if (destroy) {
            throw new IllegalStateException("The thread pool already destroy and not allow submit task.");
        }
        synchronized (TASK_QUEUE) {
            if (TASK_QUEUE.size() >= this.queueSize) {
                discardPolicy.discard();
            }
            TASK_QUEUE.addLast(runnable);
            TASK_QUEUE.notifyAll();
        }
    }

    @Override
    public void run() {
        while (!destroy) {
            System.out.printf("Pool#Min:%d,Active:%d,Max:%d,Current:%d,QueueSize:%d,ThreadQueueSize::%d
", 
                    this.min, this.active, this.max, this.size, TASK_QUEUE.size(), THREAD_QUEUE.size());
            try {
                Thread.sleep(5_000L);
                if(TASK_QUEUE.size() > this.active && this.size < this.active) {
                    for(int i = this.size; i < this.active; i++) {
                        createWorkTask();
                    }
                    System.err.printf("The pool incremented to active. currentSize:%d
", this.size);
                    this.size = this.active;
                } else if(TASK_QUEUE.size() > this.max && this.size < this.max) {
                    for(int i = this.size; i < this.max; i++) {
                        createWorkTask();
                    }
                    System.err.printf("The pool incremented to max currentSize:%d
", this.size);
                    this.size = this.max;
                }
                
                if(TASK_QUEUE.isEmpty() && this.size > this.active) {
                    System.out.println("===============Reduce===============");
                    synchronized (THREAD_QUEUE) {
                        int releaseSize = this.size - this.active;
                        for(Iterator<WorkerTask> it = THREAD_QUEUE.iterator(); it.hasNext();) {
                            if(releaseSize <= 0) {
                                break;
                            }
                            WorkerTask task = it.next();
                            task.close();
                            task.interrupt();
                            it.remove();
                            releaseSize--;
                        }
                        this.size = this.active;
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void createWorkTask() {
        WorkerTask task = new WorkerTask(GROUP, THREAD_PREFIX + (seq++));
        task.start();
        THREAD_QUEUE.add(task);
    }

    public void shutdown() throws InterruptedException {
        while (!TASK_QUEUE.isEmpty()) {
            Thread.sleep(50);
        }
        synchronized (THREAD_QUEUE) {
            int initVal = THREAD_QUEUE.size();
            while (initVal > 0) {
                for (WorkerTask task : THREAD_QUEUE) {
                    if (task.getTaskState() == TaskState.BLOCKED) {
                        task.interrupt();
                        task.close();
                        initVal--;
                    } else {
                        Thread.sleep(10);
                    }
                }
            }
        }
        this.destroy = true;
        System.out.println("The thread pool disposed.");
        System.err.println("THREAD_QUEUE size is: " + THREAD_QUEUE.size());
        System.out.println("GROUP.activeCount() is: " + GROUP.activeCount());
    }

    public int getSize() {
        return size;
    }

    public int getQueueSize() {
        return queueSize;
    }
    
    public int getMin() {
        return min;
    }

    public int getMax() {
        return max;
    }

    public int getActive() {
        return active;
    }

    public boolean isDestroy() {
        return destroy;
    }

    private enum TaskState {
        FREE, RUNNING, BLOCKED, DEAD
    }

    public static class DiscardException extends RuntimeException {

        public DiscardException(String message) {
            super(message);
        }
    }

    public interface DiscardPolicy {
        void discard() throws DiscardException;
    }

    private static class WorkerTask extends Thread {
        private volatile TaskState taskState = TaskState.FREE;

        public WorkerTask(ThreadGroup group, String name) {
            super(group, name);
        }

        public TaskState getTaskState() {
            return this.taskState;
        }

        @Override
        public void run() {
            OUTER: while (this.taskState != TaskState.DEAD) {
                Runnable runnable = null;
                synchronized (TASK_QUEUE) {
                    while (TASK_QUEUE.isEmpty()) {
                        try {
                            this.taskState = TaskState.BLOCKED;
                            TASK_QUEUE.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            System.err.println("closed.");
                            // 线程被打断回到OUTER位置
                            break OUTER;
                        }
                    }
                    runnable = TASK_QUEUE.removeFirst();
                }

                if (runnable != null) {
                    System.out.println("runnable into...");
                    this.taskState = TaskState.RUNNING;
                    runnable.run();
                    this.taskState = TaskState.FREE;
                }
            }
        }

        public void close() {
            this.taskState = TaskState.DEAD;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        SimpleThreadPool4 threadPool = new SimpleThreadPool4();
        for (int i = 0; i < 100; i++) {
            threadPool.submit(() -> {
                System.out.println("The runnable be serviced by " + Thread.currentThread() + " start.");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("The runnable be serviced by " + Thread.currentThread() + " finished.");
            });
        }
    }
}

 

以上是关于SimpleThreadPool给线程池增加自动扩充线程数量,以及闲时自动回收的功能的主要内容,如果未能解决你的问题,请参考以下文章

用Linux / C实现基于自动扩/减容线程池+epoll反应堆检测沉寂用户模型的服务器框架(含源码)

用Unix / C实现基于自动扩/减容线程池+epoll反应堆检测沉寂用户模型的服务器框架(含源码)

记一次故障引发的线程池使用的思考

线程池

为何不使用自动创建线程池呢?

面试官问:你做过什么Java线程池实践,我写了一篇博客给他看~