zoukankan      html  css  js  c++  java
  • Task 用了这么长时间的Java线程池,你不一定真的了解Java线程池 (有时间好好研究一下)

    import java.util.concurrent.LinkedBlockingQueue;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
    
    class Task implements Runnable {
        private int id;
    
        public Task(int id) {
            this.id = id;
        }
    
        @Override
        public void run() {
            System.out.println(String.format("id = %d is running.", id));
            try {
                Thread.sleep(100000000 * 100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
    
    public static void main(String[] args) {
    
            LinkedBlockingQueue<Runnable> taskPool = new LinkedBlockingQueue<>(5);
            ThreadPoolExecutor threads = new ThreadPoolExecutor(1, 4, 5L, TimeUnit.SECONDS, taskPool);
            int i = 0;
            threads.submit(new Task(++i));
            Task task = new Task(++i);
            threads.submit(task);
            for (int j = 0; j < 100; j++) {
                if(taskPool.size()==5){
                    System.out.println();
                }
                threads.submit(new Task(++i));
    
            }
            System.out.println();
        }
    

      

    备注:

     /**
         *
         * @param corePoolSize 核心数,也是线程池的基础线程数。不论线程是否限制,corePoolSize个线程不会被销毁
         * @param maximumPoolSize 最大线程数,只有当workQueue队列满了才会在corePoolSize个线程基础之上进行扩容到maximumPoolSize数;
         *                        扩容到最大数目之后workQueue还是满的话,将会报错RejectedExecutionException。当然你也可以添加拒绝策略
         * @param keepAliveTime 超过corePoolSize数目线程,闲置时间,超时被销毁。
         * @param unit 时间单位
         * @param workQueue Task队列
         */
        public  ThreadPoolExecutor(int corePoolSize,
                                  int maximumPoolSize,
                                  long keepAliveTime,
                                  TimeUnit unit,
                                  BlockingQueue<Runnable> workQueue);
    

      

    public static void main(String[] args) {
    
            RejectedExecutionHandler reject = new RejectedExecutionHandler() {
    
                @Override
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    // 各种策略吧
                    System.out.println("将任务存储到数据库或者Redis中缓冲,然后再启动一个轮询的Thread负责在空闲的时候进行再次提交!");
                }
            };
    
    
            System.out.println(reject.getClass());
            LinkedBlockingQueue<Runnable> taskPool = new LinkedBlockingQueue<>(5);
            ThreadPoolExecutor threads = new ThreadPoolExecutor(1, 4, 5L, TimeUnit.SECONDS, taskPool, new BasicThreadFactory.Builder().namingPattern("task-thread-pool-%d").build(), reject);
            int i = 0;
            threads.submit(new Task(++i));
            Task task = new Task(++i);
            threads.submit(task);
            for (int j = 0; j < 100; j++) {
                if (taskPool.size() == 5) {
                    System.out.println();
                }
                threads.submit(new Task(++i));
    
            }
            System.out.println();
        }
    

      

  • 相关阅读:
    redis 定义序列号
    mac下搭建phalcon扩展以及phalcon-devtools扩展
    rabbitmq集群架构(转载)
    mysql下limit分页优化思路
    ffmpeg图片格式转换,webp转换成jpg,webp转换成png,jpg转换成png,jpg转换成webp,png转换成webp,png转换成jpg
    sed替换多个字符串在一条命令里面
    CentOS7减轻DDOS攻击,使用fail2ban预防CC攻击
    ffmpeg改变jpg,png,webp图片大小
    wget下载github的releases的软件
    CentOS6.5设置IPTables防火墙
  • 原文地址:https://www.cnblogs.com/leodaxin/p/10951812.html
Copyright © 2011-2022 走看看