【并发编程】- 线程池使用beforeExecute()和afterExecute()监控

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第3天,点击查看活动详情

方法afterExecute()和beforeExecute()

在线程池ThreadPoolExecutor中重写这两个方法可以对线程池中执行的线程对象实现监控。

线程执行代码如下:

public class TheThread extends Thread {
    private String username;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public TheThread(String username){
        super();
        this.username=username;
    }



    @Override
    public void run() {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
            System.out.println("线程名:"+username+"  开始时间:"+simpleDateFormat.format(new Date()));
            Thread.sleep(2000);
            System.out.println("线程名:"+username+"  结束时间:"+simpleDateFormat.format(new Date()));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

自定义线程池执行代码如下:

@Slf4j
public class TheThreadPoolExecutor extends ThreadPoolExecutor {

    public TheThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        super.afterExecute(r,t);
        log.info("执行完了!");
    }

    @Override
    protected void beforeExecute(Thread t,Runnable r){
        super.beforeExecute(t,r);
        log.info("准备执行...");
    }
}

运行类代码如下:

public class BeforeExecuteAndAfterExecuteRun {
    public static void main(String[] args) {
        TheThreadPoolExecutor theThreadPoolExecutor = new TheThreadPoolExecutor(2, 2, 5, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
        for (int i = 0; i < 4; i++) {
            theThreadPoolExecutor.submit(new TheRunnable("G"+i));
        }
    }
}

运行结果如下:

14:51:27.591 [pool-1-thread-1] INFO com.ozx.concurrentprogram.executor.threadPoolExecutor.TheThreadPoolExecutor - 准备执行... 线程名:G0 开始时间:14:51:27

14:51:27.591 [pool-1-thread-2] INFO com.ozx.concurrentprogram.executor.threadPoolExecutor.TheThreadPoolExecutor - 准备执行...

线程名:G1 开始时间:14:51:27

线程名:G1 结束时间:14:51:29

线程名:G0 结束时间:14:51:29

14:51:29.605 [pool-1-thread-1] INFO com.ozx.concurrentprogram.executor.threadPoolExecutor.TheThreadPoolExecutor - 执行完了!

14:51:29.605 [pool-1-thread-2] INFO com.ozx.concurrentprogram.executor.threadPoolExecutor.TheThreadPoolExecutor - 执行完了!

14:51:29.607 [pool-1-thread-2] INFO com.ozx.concurrentprogram.executor.threadPoolExecutor.TheThreadPoolExecutor - 准备执行...

14:51:29.607 [pool-1-thread-1] INFO com.ozx.concurrentprogram.executor.threadPoolExecutor.TheThreadPoolExecutor - 准备执行...

线程名:G2 开始时间:14:51:29

线程名:G3 开始时间:14:51:29

线程名:G3 结束时间:14:51:31

线程名:G2 结束时间:14:51:31

14:51:31.621 [pool-1-thread-1] INFO com.ozx.concurrentprogram.executor.threadPoolExecutor.TheThreadPoolExecutor - 执行完了!

14:51:31.622 [pool-1-thread-2] INFO com.ozx.concurrentprogram.executor.threadPoolExecutor.TheThreadPoolExecutor - 执行完了!

从运行结果看出线程池执行任务前被beforeExecute方法监控到,而执行任务后被afterExecute方法监控,这类似于切面前后添加对应的监控日志。

猜你喜欢

转载自juejin.im/post/7126432995778494471