java ThreadPoolExecutor 异常捕获

本文详细介绍了使用Java创建线程池的过程,包括线程池参数的设置、任务创建及异常处理。通过具体代码示例,展示了如何配置核心线程数、最大线程数、线程存活时间等关键参数,并实现饱和策略和异常后的处理逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一,创建一个线程池

其中:

public void rejectedExecution(Runnable r, ThreadPoolExecutor executor)

饱和策略执行时的具体逻辑。

protected void afterExecute(Runnable r, Throwable t)

异常后的具体逻辑。

 

package com.kintech.scanAF.common;

import com.kintech.common.utils.log.LogerHelper;
import java.util.concurrent.*;

/**
 * @author Tyler
 * @date 2019/9/12
 */
public class ThreadHelper {
    //初始化线程池
    private static final ExecutorService pool = new ThreadPoolExecutor(
            2, 
            5, 
            60,
            TimeUnit.SECONDS, 
            new ArrayBlockingQueue<Runnable>(10),
            Executors.defaultThreadFactory(), 
            new ThreadPoolExecutor.DiscardPolicy(){
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            LogerHelper.Write("--- "+this.getClass().getName()+"\r\n--- 队列已满,请稍后再来");
        }
    })
    {
        @Override
        protected void afterExecute(Runnable r, Throwable t) {
            super.afterExecute(r, t);
            LogerHelper.Write(t.getMessage());
            System.out.println(t.getMessage());
        }
    };

    /**
     * 执行线程池方法(方法在RunnableFunc文件夹中)
     * @param run
     */
    public static void execute(Runnable run)
    {
        pool.execute(run);
    }
    /**
     * 执行线程池方法(方法在RunnableFunc文件夹中)
     * @param run
     */
    public static Future<?> submit(Runnable run)
    {
        Future<?> future = pool.submit(run);
        return future;
    }

}

线程池的参数介绍:

public ThreadPoolExecutor(
  int corePoolSize, // 线程数量
  int maximumPoolSize, // 最大线程数量
  long keepAliveTime, // 线程存活时间
  TimeUnit unit, //时间单位
  BlockingQueue<Runnable> workQueue, // 任务队列
  ThreadFactory threadFactory, // 线程创建工厂,可以给线程起名字
  RejectedExecutionHandler handler) // 饱和策略

 

二,创建任务

package com.kintech.scanAF.common.RunnableFunc;


/**
 * @author Tyler
 * @date 2019/9/12
 */
public class Test implements Runnable {
    private String a;
    public Test(String a)
    {
        this.a=a;
    }

    @Override
    public void run() {
        try
        {
            throw new RuntimeException( "Service has error !");
        }
        catch (Exception e) {
            throw e;
        }
        finally
        {
            a=null;
        }

    }
}

 

三,调用并获取异常

public void MainTest(String a) throws IOException {
        Future<?> future = ThreadHelper.submit(new Test(a));
        try {
            future.get();
        }
        catch (Exception ex)
        {
//记录日志
            LogerHelper.Write(ex.getMessage());
//swing弹窗
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Message", JOptionPane.ERROR_MESSAGE);
        }

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值