• ThreadPoolExecutor之三:自定义线程池-扩展示例


    ThreadPoolExecutor是可扩展的,下面一个示例:

    package com.dxz.threadpool.demo1;
    
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.atomic.AtomicLong;
    
    public class StatThreadPoolExecutor extends ThreadPoolExecutor {
    
        private final ThreadLocal<Long> startTime = new ThreadLocal<Long>();
        private final AtomicLong numTasks = new AtomicLong();
        private final AtomicLong totalTime = new AtomicLong();
    
        public StatThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
                BlockingQueue<Runnable> workQueue) {
            super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
        }
    
        @Override
        protected void beforeExecute(Thread t, Runnable r) {
            super.beforeExecute(t, r);
            System.out.println(String.format("beforeExecute() Thread '%s': start '%s'", t, r));
            startTime.set(System.nanoTime());
        }
    
        @Override
        protected void afterExecute(Runnable r, Throwable t) {
            try {
                long endTime = System.nanoTime();
                long taskTime = endTime - startTime.get();
                numTasks.incrementAndGet();
                totalTime.addAndGet(taskTime);
                System.out.println(String.format("afterExecute() : end '%s', time=%dns", r, taskTime));
            } finally {
                super.afterExecute(r, t);
            }
        }
    
        @Override
        protected void terminated() {
            try {
                System.out.println(String.format("terminated() Terminated: avg time=%dns", totalTime.get() / numTasks.get()));
            } finally {
                super.terminated();
            }
        }
    }

    启动程序:

    package com.dxz.threadpool.demo1;
    
    import java.util.concurrent.SynchronousQueue;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
    
    public class StatClient {
        public static void main(String[] args) {
            ThreadPoolExecutor exec = new StatThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
                    new SynchronousQueue<Runnable>());
            exec.execute(new Thread(new Printer(5),"t5"));
            exec.execute(new Thread(new Printer(4),"t4"));
            exec.execute(new Thread(new Printer(3),"t3"));
            exec.execute(new Thread(new Printer(2),"t2"));
            exec.execute(new Thread(new Printer(1),"t1"));
            exec.shutdown();
        }
    
    }
    
    class Printer implements Runnable {
        private int sleepTime;
    
        public Printer(int sleepTime) {
            this.sleepTime = sleepTime;
        }
    
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + " is running.");
            try {
                TimeUnit.SECONDS.sleep(sleepTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
    }

    结果:

    beforeExecute() Thread 'Thread[pool-1-thread-5,5,main]': start 'Thread[t1,5,main]'
    beforeExecute() Thread 'Thread[pool-1-thread-4,5,main]': start 'Thread[t2,5,main]'
    beforeExecute() Thread 'Thread[pool-1-thread-1,5,main]': start 'Thread[t5,5,main]'
    beforeExecute() Thread 'Thread[pool-1-thread-2,5,main]': start 'Thread[t4,5,main]'
    beforeExecute() Thread 'Thread[pool-1-thread-3,5,main]': start 'Thread[t3,5,main]'
    pool-1-thread-2 is running.
    pool-1-thread-1 is running.
    pool-1-thread-5 is running.
    pool-1-thread-4 is running.
    pool-1-thread-3 is running.
    afterExecute() : end 'Thread[t1,5,main]', time=1001000273ns
    afterExecute() : end 'Thread[t2,5,main]', time=2001157367ns
    afterExecute() : end 'Thread[t3,5,main]', time=3000630301ns
    afterExecute() : end 'Thread[t4,5,main]', time=4000804066ns
    afterExecute() : end 'Thread[t5,5,main]', time=5001279195ns
    terminated() Terminated: avg time=3000974240ns

    可以看到,在测试类client中通过execute了五个线程,然后分别对这五个线程进行统计,最后统计出各个线程的耗时平均时间。

  • 相关阅读:
    st-load视频性能测试
    gitlab-ci集成SonarQube代码质量检查
    退役记
    洛谷 P5195 【[USACO05DEC]Knights of Ni S】
    洛谷 P4742 【[Wind Festival]Running In The Sky】
    洛谷 SP13105 【MUTDNA
    洛谷 P3174 【[HAOI2009]毛毛虫】
    洛谷 P1542 【包裹快递】
    Python函数的返回值
    ubuntu18.04修改国内apt源
  • 原文地址:https://www.cnblogs.com/duanxz/p/3380367.html
Copyright © 2020-2023  润新知