Java多线程 自定义线程池详情

主要介绍:

  • 1.任务队列
  • 2.拒绝策略(抛出异常、直接丢弃、阻塞、临时队列)
  • 3.init( min )
  • 4.active
  • 5.max
min<=active<=max

package chapter13;

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

public class SimpleThreadPool {

    public final static DiscardPolicy DEFAULT_DISCARD_POLICY = () -> {
        throw new DiscardException("Discard This Task.");
    };
    private final static int DEFAULT_SIZE = 10;
    //设置线程任务最大值
    private final static int DEFAULT_TASK_QUEUE_SIZE = 2000;
    private final static LinkedList<Runnable> TASK_QUEUE = new LinkedList<>();
    private final static String THREAD_PREFIX = "SIMPLE_THREAD_POOL-";
    private final static ThreadGroup GROUP = new ThreadGroup("Pool_Group");
    private final static List<WorkerTask> THREAD_QUEUE = new ArrayList<>();
    private static volatile int seq = 0;
    private final int size;
    //队列大小
    private final int queueSize;
    private final DiscardPolicy discardPolicy;

    private volatile boolean destroy = false;

    public SimpleThreadPool() {
        this(DEFAULT_SIZE, DEFAULT_TASK_QUEUE_SIZE, DEFAULT_DISCARD_POLICY);
    }

    public SimpleThreadPool(int size, int queueSize, DiscardPolicy discardPolicy) {
        this.size = size;
        this.queueSize = queueSize;
        this.discardPolicy = discardPolicy;
        init();
    }

    private void init() {

        for (int i = 0; i < size; i++) {
            createWorkTask();
        }
    }

    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() > queueSize) discardPolicy.discard();
            TASK_QUEUE.addLast(runnable);
            TASK_QUEUE.notifyAll();
        }
    }

    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);
        }

        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.");
    }

    public int getSize() {
        return size;
    }

    public int getQueueSize() {
        return queueSize;
    }

    public boolean destory() {
        return this.destroy;
    }

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

    public interface DiscardPolicy {

        void discard() throws DiscardException;
    }

    public static class DiscardException extends RuntimeException {

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

    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;
        }

        public void run() {
            OUTER:
            while (this.taskState != TaskState.DEAD) {

                Runnable runnable;
                synchronized (TASK_QUEUE) {
                    while (TASK_QUEUE.isEmpty()) {
                        try {
                            taskState = TaskState.BLOCKED;
                            TASK_QUEUE.wait();
                        } catch (InterruptedException e) {
                            break OUTER;
                        }
                    }

                    runnable = TASK_QUEUE.removeFirst();
                }

                if (runnable != null) {
                    taskState = TaskState.RUNNING;
                    runnable.run();
                    taskState = TaskState.FREE;
                }
            }
        }

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

    public static void main(String[] args) throws InterruptedException {
        SimpleThreadPool threadPool = new SimpleThreadPool();
        for (int i = 0; i < 40; i++) {

            int finalI = i;
            threadPool.submit(() -> {
                System.out.println("The runnable " + finalI + " be serviced by " + Thread.currentThread() + " start.");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("The runnable " + finalI + " be serviced by " + Thread.currentThread() + " finished.");
            });

            System.out.println("======================");

        }
        Thread.sleep(4000);
        threadPool.shutdown();
        threadPool.submit(()->System.out.println("============="));
    }

}
======================
======================
The runnable 0 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] start.
======================
The runnable 2 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable 1 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] start.
The runnable 3 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] start.
======================
======================
======================
The runnable 5 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable 6 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] start.
======================
======================
The runnable 7 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable 4 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable 8 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] start.
======================
======================
======================
======================
The runnable 9 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] start.
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
======================
The runnable 7 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable 10 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable 6 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] finished.
The runnable 11 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] start.
The runnable 3 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] finished.
The runnable 12 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] start.
The runnable 5 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable 0 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] finished.
The runnable 14 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] start.
The runnable 2 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable 15 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable 8 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] finished.
The runnable 16 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] start.
The runnable 4 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable 17 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable 9 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] finished.
The runnable 1 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] finished.
The runnable 19 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] start.
The runnable 18 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] start.
The runnable 13 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable 10 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable 14 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] finished.
The runnable 11 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] finished.
The runnable 22 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] start.
The runnable 12 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] finished.
The runnable 21 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] start.
The runnable 20 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable 23 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] start.
The runnable 15 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable 24 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable 17 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable 18 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] finished.
The runnable 25 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable 13 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable 16 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] finished.
The runnable 19 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] finished.
The runnable 28 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] start.
The runnable 27 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable 26 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] start.
The runnable 29 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] start.
The runnable 22 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] finished.
The runnable 30 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] start.
The runnable 20 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable 31 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable 23 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] finished.
The runnable 21 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] finished.
The runnable 32 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] start.
The runnable 33 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] start.
The runnable 24 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable 34 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable 25 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable 29 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] finished.
The runnable 27 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable 35 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable 26 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] finished.
The runnable 28 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] finished.
The runnable 39 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] start.
The runnable 38 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] start.
The runnable 37 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable 36 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] start.
The runnable 33 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] finished.
The runnable 32 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] finished.
The runnable 31 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable 30 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] finished.
The runnable 39 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] finished.
The runnable 34 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable 38 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] finished.
The runnable 36 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] finished.
The runnable 37 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable 35 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The thread pool disposed.
Exception in thread "main" java.lang.IllegalStateException: The thread pool already destroy and not allow submit task.
    at chapter13.SimpleThreadPool.submit(SimpleThreadPool.java:48)
    at chapter13.SimpleThreadPool.main(SimpleThreadPool.java:176)

线程池本身就是一个线程:

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

public class SimpleThreadPool extends Thread {

    public final static DiscardPolicy DEFAULT_DISCARD_POLICY = () -> {
        throw new DiscardException("Discard This Task.");
    };
    //设置线程任务最大值
    private final static int DEFAULT_TASK_QUEUE_SIZE = 2000;
    /**
     * 任务队列
     */
    private final static LinkedList<Runnable> TASK_QUEUE = new LinkedList<>();
    private final static String THREAD_PREFIX = "SIMPLE_THREAD_POOL-";
    /**
     * 线程组
     */
    private final static ThreadGroup GROUP = new ThreadGroup("Pool_Group");
    private final static List<WorkerTask> THREAD_QUEUE = new ArrayList<>();
    private static volatile int seq = 0;
    //队列大小
    private final int queueSize;
    private final DiscardPolicy discardPolicy;
    private int size;
    private volatile boolean destroy = false;

    /**
     * 最小线程数
     */
    private int min;
    /**
     * 最大线程数
     */
    private int max;
    /**
     * 线程活跃数
     */
    private int active;

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

    public SimpleThreadPool(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() > queueSize) discardPolicy.discard();
            TASK_QUEUE.addLast(runnable);
            TASK_QUEUE.notifyAll();
        }
    }

    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.");
    }

    public int getSize() {
        return size;
    }

    public int getQueueSize() {
        return queueSize;
    }

    public boolean isDestory() {
        return this.destroy;
    }

    public int getMin() {
        return min;
    }

    public int getMax() {
        return max;
    }

    public int getActive() {
        return active;
    }

    @Override
    public void run() {
        while (!destroy) {
            System.out.printf("Pool#Min:%d,Active:%d,Max:%d,Current:%d,QueueSize:%d\n",
                    this.min, this.active, this.max, this.size, TASK_QUEUE.size());

            try {
                Thread.sleep(5_000L);
                //让线程池以最大活跃数运行
                if (TASK_QUEUE.size() > active && size < active) {
                    for (int i = size; i < active; i++) {
                        createWorkTask();
                    }
                    System.out.println("The pool incremented to active.");

                    size = active;
                    //让线程池以最大线程数运行
                } else if (TASK_QUEUE.size() > max && size < max) {
                    for (int i = size; i < max; i++) {
                        createWorkTask();
                    }
                    System.out.println("The pool incremented to max.");
                    //让size以最大活跃数运行
                    size = max;
                }

                /**
                 * 释放线程
                 * 加锁 防止submit的时候 有其他的操作
                 */
                synchronized (TASK_QUEUE) {
                    if (TASK_QUEUE.isEmpty() && size > active) {
                        System.out.println("==========Reduce=========");
                        int releaseSize = size - 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--;
                        }

                        size = active;
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

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

    public interface DiscardPolicy {

        void discard() throws DiscardException;
    }

    public static class DiscardException extends RuntimeException {

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

    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;
        }

        public void run() {
            OUTER:
            while (this.taskState != TaskState.DEAD) {

                Runnable runnable;
                synchronized (TASK_QUEUE) {
                    while (TASK_QUEUE.isEmpty()) {
                        try {
                            taskState = TaskState.BLOCKED;
                            TASK_QUEUE.wait();
                        } catch (InterruptedException e) {
                            System.out.println("Closed.");
                            break OUTER;
                        }
                    }

                    runnable = TASK_QUEUE.removeFirst();
                }

                if (runnable != null) {
                    taskState = TaskState.RUNNING;
                    runnable.run();
                    taskState = TaskState.FREE;
                }
            }
        }

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

    public static void main(String[] args) throws InterruptedException {
        SimpleThreadPool threadPool = new SimpleThreadPool();
        for (int i = 0; i < 40; i++) {

            int finalI = i;
            threadPool.submit(() -> {
                System.out.println("The runnable " + finalI + " be serviced by " + Thread.currentThread() + " start.");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("The runnable " + finalI + " be serviced by " + Thread.currentThread() + " finished.");
            });

            //System.out.println("======================");

        }
//        Thread.sleep(4000);
//        threadPool.shutdown();
//        threadPool.submit(() -> System.out.println("============="));
    }
}
Pool#Min:4,Active:8,Max:12,Current:4,QueueSize:0
The runnable 2 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable 3 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable 1 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable 0 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable 3 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable 4 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable 1 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable 5 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable 2 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable 6 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable 0 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable 7 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable 8 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] start.
The runnable 9 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] start.
The pool incremented to active.
The runnable 10 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] start.
Pool#Min:4,Active:8,Max:12,Current:8,QueueSize:29
The runnable 11 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] start.
The runnable 4 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable 12 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable 7 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable 13 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable 5 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable 6 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable 14 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable 15 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable 9 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] finished.
The runnable 16 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] start.
The runnable 8 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] finished.
The runnable 10 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] finished.
The runnable 18 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] start.
The runnable 17 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] start.
The runnable 11 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] finished.
The runnable 19 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] start.
The runnable 14 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable 20 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable 15 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable 13 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable 22 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable 12 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable 21 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable 23 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable 24 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] start.
The runnable 25 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] start.
The runnable 26 be serviced by Thread[SIMPLE_THREAD_POOL-10,5,Pool_Group] start.
The pool incremented to max.
Pool#Min:4,Active:8,Max:12,Current:12,QueueSize:13
The runnable 27 be serviced by Thread[SIMPLE_THREAD_POOL-11,5,Pool_Group] start.
The runnable 18 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] finished.
The runnable 19 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] finished.
The runnable 29 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] start.
The runnable 16 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] finished.
The runnable 30 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] start.
The runnable 17 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] finished.
The runnable 31 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] start.
The runnable 28 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] start.
The runnable 20 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable 32 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable 23 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable 33 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable 21 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable 22 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable 34 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable 35 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable 24 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] finished.
The runnable 25 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] finished.
The runnable 36 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] start.
The runnable 26 be serviced by Thread[SIMPLE_THREAD_POOL-10,5,Pool_Group] finished.
The runnable 37 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] start.
The runnable 38 be serviced by Thread[SIMPLE_THREAD_POOL-10,5,Pool_Group] start.
The runnable 27 be serviced by Thread[SIMPLE_THREAD_POOL-11,5,Pool_Group] finished.
The runnable 39 be serviced by Thread[SIMPLE_THREAD_POOL-11,5,Pool_Group] start.
The runnable 29 be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] finished.
The runnable 31 be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] finished.
The runnable 30 be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] finished.
The runnable 28 be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] finished.
==========Reduce=========
The runnable 35 be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable 32 be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable 33 be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable 34 be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
Closed.
Pool#Min:4,Active:8,Max:12,Current:8,QueueSize:0
Closed.
Closed.
Closed.
The runnable 38 be serviced by Thread[SIMPLE_THREAD_POOL-10,5,Pool_Group] finished.
The runnable 36 be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] finished.
The runnable 37 be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] finished.
The runnable 39 be serviced by Thread[SIMPLE_THREAD_POOL-11,5,Pool_Group] finished.
Pool#Min:4,Active:8,Max:12,Current:8,QueueSize:0
Pool#Min:4,Active:8,Max:12,Current:8,QueueSize:0
Pool#Min:4,Active:8,Max:12,Current:8,QueueSize:0

到此这篇关于Java多线程 自定义线程池详情的文章就介绍到这了,更多相关Java多线程 自定义线程池内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • java中多线程与线程池的基本使用方法

    目录 前言 继承Thread 实现Runnale接口 Callable 线程池 常见的4种线程池. 总结 前言 在java中,如果每个请求到达就创建一个新线程,开销是相当大的.在实际使用中,服务器在创建和销毁线程上花费的时间和消耗的系统资源都相当大,甚至可能要比在处理实际的用户请求的时间和资源要多的多.除了创建和销毁线程的开销之外,活动的线程也需要消耗系统资源.如果在一个jvm里创建太多的线程,可能会使系统由于过度消耗内存或"切换过度"而导致系统资源不足.为了防止资源不足,服务器应用程

  • Java多线程导致CPU占用100%解决及线程池正确关闭方式

    简介 情景:1000万表数据导入内存数据库,按分页大小10000查询,多线程,15条线程跑. 使用了ExecutorService executor = Executors.newFixedThreadPool(15) 本地跑了一段时间后,发现电脑CPU逐渐升高,最后CPU占用100%卡死,内存使用也高达80%. 排查问题 Debug 发现虽然创建了定长15的线程池,但是因为数据量大,在For中循环分页查询的List会持续加入LinkedBlockingQueue() 队列中每一个等待的任务,又

  • java多线程CountDownLatch与线程池ThreadPoolExecutor/ExecutorService案例

    1.CountDownLatch: 一个同步工具类,它允许一个或多个线程一直等待,直到其他线程的操作执行完后再执行. 2.ThreadPoolExecutor/ExecutorService: 线程池,使用线程池可以复用线程,降低频繁创建线程造成的性能消耗,同时对线程的创建.启动.停止.销毁等操作更简便. 3.使用场景举例: 年末公司组织团建,要求每一位员工周六上午8点到公司门口集合,统一乘坐公司所租大巴前往目的地. 在这个案例中,公司作为主线程,员工作为子线程. 4.代码示例: package

  • java简单实现多线程及线程池实例详解

    本文为大家分享了java多线程的简单实现及线程池实例,供大家参考,具体内容如下 一.多线程的两种实现方式 1.继承Thread类的多线程 /** * 继承Thread类的多线程简单实现 */ public class extThread extends Thread { public void run(){ for(int i=0;i<100;i++){ System.out.println(getName()+"-"+i); } } public static void mai

  • java多线程教程之如何使用线程池详解

    为什么要用线程池? 诸如 Web 服务器.数据库服务器.文件服务器或邮件服务器之类的许多服务器应用程序都面临处理来自某些远程来源的大量短小的任务.请求以某种方式到达服务器,这种方式可能是通过网络协议(例如 HTTP.FTP 或 POP).通过 JMS 队列或者可能通过轮询数据库.不管请求如何到达,服务器应用程序中经常出现的情况是:单个任务处理的时间很短而请求的数目却是巨大的. 只有当任务都是同类型并且相互独立时,线程池的性能才能达到最佳.如果将运行时间较长的与运行时间较短的任务混合在一起,那么除

  • java多线程学习笔记之自定义线程池

    当我们使用 线程池的时候,可以使用 newCachedThreadPool()或者 newFixedThreadPool(int)等方法,其实我们深入到这些方法里面,就可以看到它们的是实现方式是这样的. public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueu

  • Java多线程之线程池七个参数详解

    ThreadPoolExecutor是JDK中的线程池实现,这个类实现了一个线程池需要的各个方法,它提供了任务提交.线程管理.监控等方法. 下面是ThreadPoolExecutor类的构造方法源码,其他创建线程池的方法最终都会导向这个构造方法,共有7个参数:corePoolSize.maximumPoolSize.keepAliveTime.unit.workQueue.threadFactory.handler. public ThreadPoolExecutor(int corePoolS

  • 一文彻底搞懂java多线程和线程池

    目录 什么是线程 一. Java实现线程的三种方式 1.1.继承Thread类 1.2.实现Runnable接口,并覆写run方法 二. Callable接口 2.1 Callable接口 2.2 Future接口 2.3 Future实现类是FutureTask. 三. Java线程池 3.1.背景 3.2.作用 3.3.应用范围 四. Java 线程池框架Executor 4.1.类图: 4.2 核心类ThreadPoolExecutor: 4.3 ThreadPoolExecutor逻辑结

  • Java多线程 自定义线程池详情

    主要介绍: 1.任务队列 2.拒绝策略(抛出异常.直接丢弃.阻塞.临时队列) 3.init( min ) 4.active 5.max min<=active<=max package chapter13; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class SimpleThreadPool { public final static DiscardPolicy

  • Java多线程与线程池技术分享

    目录 一.序言 1.普通执行 2.线程池执行 二.线程池基础 1.核心参数 2.参数与池的关系 1.通用对比 2.拓展对比 3.无返回值任务 4.有返回值任务 三.Executors 1.创建单一线程的线程池 2.创建固定数量的线程池 3.创建可伸缩的线程池 4.创建定时调度的线程池 四.手动创建线程池 五.其它 1.配置线程池的参数 2.线程池监控 六.总结 一.序言 Java多线程编程线程池被广泛使用,甚至成为了标配. 线程池本质是池化技术的应用,和连接池类似,创建连接与关闭连接属于耗时操作

  • Java多线程之线程安全问题详情

    目录 1.线程安全概述 1.1什么是线程安全问题 1.2一个存在线程安全问题的程序 2.线程加锁与线程不安全的原因 2.1案例分析 2.2线程加锁 2.2.1什么是加锁 2.2.2如何加锁 2.2.3再析案例 2.3线程不安全的原因 3.线程安全的标准类 4.Object类提供的线程等待方法 前言: 本篇文章介绍的内容为Java多线程中的线程安全问题,此处的安全问题并不是指的像黑客入侵造成的安全问题,线程安全问题是指因多线程抢占式执行而导致程序出现bug的问题. 1.线程安全概述 1.1什么是线

  • Java如何自定义线程池中队列

    目录 背景 问题分析 问题解决 总结 两个队列的UML关系图 SynchronousQueue的定义 ArrayBlockingQueue的定义 分析 jdk源码中关于线程池队列的说明 背景 业务交互的过程中涉及到了很多关于SFTP下载的问题,因此在代码中定义了一些线程池,使用中发现了一些问题, 代码类似如下所示: public class ExecutorTest { private static ExecutorService es = new ThreadPoolExecutor(2, 1

  • JAVA 自定义线程池的最大线程数设置方法

    一:CPU密集型: 定义:CPU密集型也是指计算密集型,大部分时间用来做计算逻辑判断等CPU动作的程序称为CPU密集型任务.该类型的任务需要进行大量的计算,主要消耗CPU资源.  这种计算密集型任务虽然也可以用多任务完成,但是任务越多,花在任务切换的时间就越多,CPU执行任务的效率就越低,所以,要最高效地利用CPU,计算密集型任务同时进行的数量应当等于CPU的核心数. 特点:    01:CPU 使用率较高(也就是经常计算一些复杂的运算,逻辑处理等情况)非常多的情况下使用    02:针对单台机

随机推荐