• 自定义线程池


    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.*;
    import java.util.concurrent.atomic.AtomicInteger;
    
    public class ProducerConsumer {
        private final static Logger logger = LoggerFactory.getLogger(ProducerConsumer.class);
        /**
         *  球的容器(原始的)
         */
        private static List<String> balls = new ArrayList<>(15);
    
        private  static  ConcurrentLinkedQueue<String> ballsEx = new ConcurrentLinkedQueue<String>();
    
        /**
         *  球的容器(处理过的)
         */
        private static List<String> ballsDone = new ArrayList<>(15);
    
        /**
         * 通过自定义ThreadFactory设置线程的名称; (便于在调试的时候识别它们)
         */
        static class RobotMainThreadFactory implements ThreadFactory {
            private AtomicInteger count = new AtomicInteger(0);
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                String threadName = "moveball_" + count.addAndGet(1);
                t.setName(threadName);
                return t;
            }
        }
    
        /**  任务线程池 **/
        private static ThreadPoolExecutor threadPool = null;
        private static void createThreadPool(){
            int maximumPoolSize = 5;
            threadPool = new ThreadPoolExecutor(maximumPoolSize,  maximumPoolSize,  24L,
                    TimeUnit.HOURS, new ArrayBlockingQueue<Runnable>(maximumPoolSize + 2),
                    new RobotMainThreadFactory());
            RejectedExecutionHandler handler = new RejectedExecutionHandler() {
                @Override
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    logger.error("主线程池已满, 无法添加任务: " + r.toString());
                }
            };
            threadPool.setRejectedExecutionHandler(handler);
            logger.info("成功创建线程池 robotMainThreadPool.");
        }
    
        public static void main(String[] args) throws InterruptedException {
            balls.add("aa");
            balls.add("bb");
            balls.add("cc");
            balls.add("dd");
            balls.add("ee");
            balls.add("ff");
            balls.add("gg");
            balls.add("hh");
            balls.add("ii");
            createThreadPool();
    
            for(int i=0; i <= 4; i++) {
                Runnable task = new Runnable() {
                    @Override
                    public void run() {
                        while (balls.size() != 0) {
                            String oneBall = "";
                            ballsEx.poll();
    
                            synchronized (balls) {
                                oneBall = balls.get(0);
                                balls.remove(0);
                            }
                            if(oneBall.length() == 0) {
                                break;
                            }
                            logger.info("我在搬运小球:" + oneBall);
                            synchronized (ballsDone) {
                                ballsDone.add(oneBall);
                            }
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                };
                threadPool.execute(task);
            }
    
            Thread.sleep(5000);
    
            System.out.println(balls.size() + " , " + ballsDone.size());
    
        }
    
    }
  • 相关阅读:
    Java学习笔记
    JSP/Servlet笔记
    JavaScript笔记
    JavaScript笔记
    JavaScript笔记 – 程序语法设计
    Mybatis笔记
    Mybatis笔记
    Mybatis笔记 – 关联查询
    Mybatis笔记 – Po映射类型
    Mybatis笔记
  • 原文地址:https://www.cnblogs.com/mihich/p/13306681.html
Copyright © 2020-2023  润新知