joe 发表于 2021-12-17 10:17:28

线程池代码实验


import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* @Author Joe_Liang
* @Date 2021/12/17 9:42
* @Version 1.0
*/
public class MonitorThreadPool extends ThreadPoolExecutor {
    public MonitorThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
      super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    @Override
    public void execute(Runnable command) {
      super.execute(command);//如果将super.execute屏幕掉,则beforeExecute和afterExecute都不会执行了
    }

    @Override
    public void shutdown() {
      System.out.println("shutdown....");
      monitor();
      super.shutdown();
    }

    @Override
    protected void beforeExecute(Thread t, Runnable r) {
      System.out.println("beforeExecute....");
      monitor();
      super.beforeExecute(t, r);
    }

    @Override
    protected void afterExecute(Runnable r, Throwable t) {
      System.out.println("afterExecute....");
      monitor();
      super.afterExecute(r, t);
    }

    @Override
    protected void terminated() {
      System.out.println("terminated....");
      monitor();
      super.terminated();
    }
    public void monitor(){
      System.out.println("当前激活线程数量:" + getActiveCount());
      System.out.println("当前存在的线程数:" + getPoolSize());
      System.out.println("历史最大的线程数:" + getMaximumPoolSize());
      System.out.println("已提交的任务数:" + getTaskCount());
      System.out.println("已完成的任务数:" + getCompletedTaskCount());
      System.out.println("队列中的任务数:" + getQueue().size());
    }
}


import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
* @Author Joe_Liang
* @Date 2021/12/17 9:49
* @Version 1.0
*/
public class MonitorThreadPoolMain {//原文链接:https://blog.csdn.net/gorhaf/article/details/119281153
    public static void main(String[] args)
    {
      MonitorThreadPool threadPool = new MonitorThreadPool(1,3,0,TimeUnit.SECONDS,new LinkedBlockingQueue<>(2));
      try {
            // 提交多个任务
            for (int i = 5; i > 0; i--) {
                // 创建任务
                Task task = new Task(i);
                // 提交任务
                threadPool.submit(task);
                // 每隔500毫秒提交一个
                Thread.sleep(500);
            }
            // 使主线程休眠6秒钟
            Thread.sleep(6000);
            // 关闭线程池之前获取一次情况
            System.out.println("-------------");
            threadPool.monitor();
      } catch (InterruptedException e) {
            e.printStackTrace();
      } finally {
            // 关闭线程池
            System.out.println("++++++++++++++++");
            threadPool.shutdown();
      }
    }
}
class Task implements Runnable {
    /**
   * 执行时间
   */
    private int timeout;

    public Task(int timeout) {
      this.timeout = timeout;
    }

    @Override
    public void run() {
      try {
            // 使当前线程休眠指定时间
            Thread.sleep(timeout * 1000L);
      } catch (InterruptedException e) {
            e.printStackTrace();
      }
    }
}

打印结果:
beforeExecute....
当前激活线程数量:1
当前存在的线程数:1
历史最大的线程数:3
已提交的任务数:1
已完成的任务数:0
队列中的任务数:0
beforeExecute....
当前激活线程数量:2
当前存在的线程数:2
历史最大的线程数:3
已提交的任务数:4
已完成的任务数:0
队列中的任务数:2
beforeExecute....
当前激活线程数量:3
当前存在的线程数:3
历史最大的线程数:3
已提交的任务数:5
已完成的任务数:0
队列中的任务数:2
afterExecute....
当前激活线程数量:3
当前存在的线程数:3
历史最大的线程数:3
已提交的任务数:5
已完成的任务数:0
队列中的任务数:2
beforeExecute....
当前激活线程数量:3
当前存在的线程数:3
历史最大的线程数:3
已提交的任务数:5
已完成的任务数:1
队列中的任务数:1
afterExecute....
当前激活线程数量:3
当前存在的线程数:3
历史最大的线程数:3
已提交的任务数:5
已完成的任务数:1
队列中的任务数:1
beforeExecute....
当前激活线程数量:3
当前存在的线程数:3
历史最大的线程数:3
已提交的任务数:5
已完成的任务数:2
队列中的任务数:0
afterExecute....
当前激活线程数量:3
当前存在的线程数:3
历史最大的线程数:3
已提交的任务数:5
已完成的任务数:2
队列中的任务数:0
afterExecute....
当前激活线程数量:2
当前存在的线程数:2
历史最大的线程数:3
已提交的任务数:5
已完成的任务数:3
队列中的任务数:0
afterExecute....
当前激活线程数量:1
当前存在的线程数:1
历史最大的线程数:3
已提交的任务数:5
已完成的任务数:4
队列中的任务数:0
-------------
当前激活线程数量:0
当前存在的线程数:1
历史最大的线程数:3
已提交的任务数:5
已完成的任务数:5
队列中的任务数:0
++++++++++++++++
shutdown....
当前激活线程数量:0
当前存在的线程数:1
历史最大的线程数:3
已提交的任务数:5
已完成的任务数:5
队列中的任务数:0
terminated....
当前激活线程数量:0
当前存在的线程数:0
历史最大的线程数:3
已提交的任务数:5
已完成的任务数:5
队列中的任务数:0


页: [1]
查看完整版本: 线程池代码实验