• SimpleThreadPool极简版


    package com.dwz.concurrency.chapter13;
    
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    
    /**
     * 1.任务队列--调度 2.拒绝策略(抛出异常、直接丢弃、阻塞、临时队列)--性能保护 3.init(min) 4.active 5.max
     * min<=active<=max Quartz/Control-M
     */
    public class SimpleThreadPool {
        private final int size;
        private final static int DEFAULT_SIZE = 10;
        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<>();
    
        public SimpleThreadPool() {
            this(DEFAULT_SIZE);
        }
    
        public SimpleThreadPool(int size) {
            this.size = size;
            init();
        }
    
        private void init() {
            for (int i = 0; i < this.size; i++) {
                createWorkTask();
            }
        }
    
        public void submit(Runnable runnable) {
            synchronized (TASK_QUEUE) {
                TASK_QUEUE.addLast(runnable);
                TASK_QUEUE.notifyAll();
            }
        }
    
        private void createWorkTask() {
            WorkerTask task = new WorkerTask(GROUP, THREAD_PREFIX + (seq++));
            task.start();
            THREAD_QUEUE.add(task);
        }
    
        private enum TaskState {
            FREE, RUNNING, BLOCKED, DEAD
        }
    
        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();
                                // 线程被打断回到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) {
            SimpleThreadPool threadPool = new SimpleThreadPool();
    //        IntStream.range(0, 40).forEach(i -> {
    //            threadPool.submit(() -> {
    //                System.out.println("The runnable " + i + " be serviced by " + Thread.currentThread() + " start.");
    //                try {
    //                    Thread.sleep(1000);
    //                } catch (InterruptedException e) {
    //                    e.printStackTrace();
    //                }
    //                System.out.println("The runnable " + i + " be serviced by " + Thread.currentThread() + " finished.");
    //            });
    //        });
            for(int i = 0; i < 40; 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.");
                });
            }
        }
    }

    该线程池只创建了默认的线程数量,不包含拒绝策略,线程池的打断、销毁和自动扩展线程数量,只保持了线程的最低性能,只是极简版,之后的文章会慢慢扩展

    可参考文章:

    https://blog.csdn.net/const_/article/details/89317907

  • 相关阅读:
    [转帖]活用Quartus II内置模板,快速输入HDL代码、TimeQuset束缚及tcl语句等
    [笔记] FPGA的发展
    [转帖]状态机的编码
    [笔记]Altera中DDR3设计
    [笔记]Test Plan的编写 及 程序开头注释
    [HDOJ2457]DNA repair
    [HDOJ2355]The Sidewinder Sleeps Tonite
    [HDOJ2825]Wireless Password
    [HDOJ2222]Keywords Search
    [HDOJ2454]Degree Sequence of Graph G
  • 原文地址:https://www.cnblogs.com/zheaven/p/12085063.html
Copyright © 2020-2023  润新知