高并发编程-自定义简易的线程池(2),体会原理

简介: 高并发编程-自定义简易的线程池(2),体会原理

20191031000702451.png

概述

高并发编程-自定义简易的线程池(1),体会原理 中只实现了任务队列,我们这里把其余的几个也补充进来

  • 拒绝策略
  • 关闭线程池
  • 最小 最大 活动线程数


示例

比较简单,直接上代码,见注释

package com.artisan.test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class SimpleThreadPool extends Thread {
    // 线程数
    private 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;
    // 默认拒绝策略
    public 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 SimpleThreadPool() {
        // 调用有参构造函数
        this(4, 8, 12, DEFAULT_TASK_QUEUE_SIZE, DEFAULT_DISCARD_POLICY);
    }
    /**
     * 构造函数
     * @param min
     * @param active
     * @param max
     * @param queueSize
     * @param discardPolicy
     */
    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();
    }
    /**
     * 提交任务到 TASK_QUEUE
     * @param runnable
     */
    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();
        }
    }
    /**
     * 管理线程池本身的参数
     */
    @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 = max;
                }
                synchronized (THREAD_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 void createWorkTask() {
        WorkerTask task = new WorkerTask(GROUP, THREAD_PREFIX + (seq++));
        task.start();
        THREAD_QUEUE.add(task);
    }
    /**
     * 关闭线程池
     * @throws InterruptedException
     */
    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);
                    }
                }
            }
        }
        System.out.println(GROUP.activeCount());
        this.destroy = true;
        System.out.println("The thread pool disposed.");
    }
    public int getQueueSize() {
        return queueSize;
    }
    public int getSize() {
        return size;
    }
    public boolean isDestroy() {
        return this.destroy;
    }
    public int getMin() {
        return min;
    }
    public int getMax() {
        return max;
    }
    public int getActive() {
        return active;
    }
    /**
     * 工作线程的状态
     */
    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;
        }
        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;
        }
    }
    /**
     * 测试
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        SimpleThreadPool threadPool = new SimpleThreadPool();
        for (int i = 0; i < 40; i++) {
            threadPool.submit(() -> {
                System.out.println("The runnable  be serviced by " + Thread.currentThread() + " start.");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("The runnable be serviced by " + Thread.currentThread() + " finished.");
            });
        }
        Thread.sleep(10000);
        threadPool.shutdown();
       /* Thread.sleep(10000);
        threadPool.shutdown();
        threadPool.submit(() -> System.out.println("======="));*/
    }
}


运行日志:

"E:\Program Files\Java\jdk1.8.0_161\bin\java" "-javaagent:E:\Program Files\JetBrains\IntelliJ IDEA 2017.2.4\lib\idea_rt.jar=53638:E:\Program Files\JetBrains\IntelliJ IDEA 2017.2.4\bin" -Dfile.encoding=UTF-8 -classpath "E:\Program Files\Java\jdk1.8.0_161\jre\lib\charsets.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\deploy.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\access-bridge-64.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\cldrdata.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\dnsns.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jaccess.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jfxrt.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\localedata.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\nashorn.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunec.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunjce_provider.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunmscapi.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunpkcs11.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\zipfs.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\javaws.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\jce.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\jfr.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\jfxswt.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\jsse.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\management-agent.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\plugin.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\resources.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\rt.jar;D:\IdeaProjects\mvc\target\classes" com.artisan.test.SimpleThreadPool
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
Pool#Min:4,Active:8,Max:12,Current:4,QueueSize:38
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The pool incremented to active.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] start.
Pool#Min:4,Active:8,Max:12,Current:8,QueueSize:31
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] start.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] start.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] finished.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] finished.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] finished.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] start.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] start.
The pool incremented to max.
Pool#Min:4,Active:8,Max:12,Current:12,QueueSize:15
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] start.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-10,5,Pool_Group] start.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-11,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] finished.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] finished.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] finished.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] start.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] finished.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] start.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-10,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-11,5,Pool_Group] finished.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-10,5,Pool_Group] start.
The runnable  be serviced by Thread[SIMPLE_THREAD_POOL-11,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] finished.
Closed.
Closed.
Closed.
Closed.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
Closed.
Closed.
Closed.
Closed.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-10,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-11,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] finished.
Closed.
4
=========Reduce========
The thread pool disposed.
Closed.
Closed.
Closed.
Process finished with exit code 0



相关文章
|
18天前
|
存储 安全 Java
Java多线程编程秘籍:各种方案一网打尽,不要错过!
Java 中实现多线程的方式主要有四种:继承 Thread 类、实现 Runnable 接口、实现 Callable 接口和使用线程池。每种方式各有优缺点,适用于不同的场景。继承 Thread 类最简单,实现 Runnable 接口更灵活,Callable 接口支持返回结果,线程池则便于管理和复用线程。实际应用中可根据需求选择合适的方式。此外,还介绍了多线程相关的常见面试问题及答案,涵盖线程概念、线程安全、线程池等知识点。
104 2
|
1月前
|
缓存 Java 调度
多线程编程核心:上下文切换深度解析
在现代计算机系统中,多线程编程已成为提高程序性能和响应速度的关键技术。然而,多线程编程中一个不可避免的概念就是上下文切换(Context Switching)。本文将深入探讨上下文切换的概念、原因、影响以及优化策略,帮助你在工作和学习中深入理解这一技术干货。
51 10
|
1月前
|
安全 算法 Java
Java多线程编程中的陷阱与最佳实践####
本文探讨了Java多线程编程中常见的陷阱,并介绍了如何通过最佳实践来避免这些问题。我们将从基础概念入手,逐步深入到具体的代码示例,帮助开发者更好地理解和应用多线程技术。无论是初学者还是有经验的开发者,都能从中获得有价值的见解和建议。 ####
|
1月前
|
Java 调度
Java中的多线程编程与并发控制
本文深入探讨了Java编程语言中多线程编程的基础知识和并发控制机制。文章首先介绍了多线程的基本概念,包括线程的定义、生命周期以及在Java中创建和管理线程的方法。接着,详细讲解了Java提供的同步机制,如synchronized关键字、wait()和notify()方法等,以及如何通过这些机制实现线程间的协调与通信。最后,本文还讨论了一些常见的并发问题,例如死锁、竞态条件等,并提供了相应的解决策略。
55 3
|
1月前
|
算法 调度 开发者
多线程编程核心:上下文切换深度解析
在多线程编程中,上下文切换是一个至关重要的概念,它直接影响到程序的性能和响应速度。本文将深入探讨上下文切换的含义、原因、影响以及如何优化,帮助你在工作和学习中更好地理解和应用多线程技术。
45 4
|
15天前
|
安全 Java API
【JavaEE】多线程编程引入——认识Thread类
Thread类,Thread中的run方法,在编程中怎么调度多线程
|
2月前
|
安全 Java 开发者
Java中的多线程编程:从基础到实践
本文深入探讨了Java多线程编程的核心概念和实践技巧,旨在帮助读者理解多线程的工作原理,掌握线程的创建、管理和同步机制。通过具体示例和最佳实践,本文展示了如何在Java应用中有效地利用多线程技术,提高程序性能和响应速度。
72 1
|
18天前
|
NoSQL Redis
单线程传奇Redis,为何引入多线程?
Redis 4.0 引入多线程支持,主要用于后台对象删除、处理阻塞命令和网络 I/O 等操作,以提高并发性和性能。尽管如此,Redis 仍保留单线程执行模型处理客户端请求,确保高效性和简单性。多线程仅用于优化后台任务,如异步删除过期对象和分担读写操作,从而提升整体性能。
48 1
|
3月前
|
存储 消息中间件 资源调度
C++ 多线程之初识多线程
这篇文章介绍了C++多线程的基本概念,包括进程和线程的定义、并发的实现方式,以及如何在C++中创建和管理线程,包括使用`std::thread`库、线程的join和detach方法,并通过示例代码展示了如何创建和使用多线程。
65 1
|
3月前
|
Java 开发者
在Java多线程编程中,创建线程的方法有两种:继承Thread类和实现Runnable接口
【10月更文挑战第20天】在Java多线程编程中,创建线程的方法有两种:继承Thread类和实现Runnable接口。本文揭示了这两种方式的微妙差异和潜在陷阱,帮助你更好地理解和选择适合项目需求的线程创建方式。
47 3