线程池的异常处理

一、Java线程异常机制

在java多线程程序中,所有线程都不允许抛出未捕获的checked exception,也就是说各个线程需要自己将checked exception处理掉,run方法上面进行了约束,不可以抛出异常(throws Exception)

public class Task implements Runnable {
    @Override
    public void run() {  //run()方法上不可以抛出异常
        System.out.println("任务开始执行");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        int i = 10 /0;  //这里抛出RuntimeException()
        System.out.println("任务执行结束");
    }
}

public class ThreadException {

    public static void main(String[] args) {

        Thread t1 = new Thread(new Task());
        //t1.setUncaughtExceptionHandler(new RuntimeExceptionHandle());

        t1.start();
        System.out.println("主线程执行结束!!!");
    }
}


运行结果:
主线程执行结束!!!
任务开始执行
Exception in thread “Thread-0” java.lang.ArithmeticException: / by zero
at com.ljj.threadException.Task.run(Task.java:30)
at java.lang.Thread.run(Thread.java:748)

t1线程运行抛出异常不会影响主线程的执行,当此类异常产生时,子线程就会终结。我们无法在主线程中捕获子线程抛出的异常进行处理,只能在run方法内部对业务逻辑进行try/catch。线程是独立执行的代码片断,线程的问题应该由线程自己来解决,而不要委托到外部。

如果不在run()内部捕获异常,该怎么处理呢?

可以使用Thread.UncaughtExceptionHandler为每个线程设置异常处理器,Thread.UncaughtExceptionHandler.uncaughtException()方法会在线程因未捕获的异常而面临死亡时被调用,上述子线程本身因为异常终止打印到控制台也是由于UncaughtExceptionHandler

实现UncaughtExceptionHandler接口并重写uncaughtException方法,在uncaughtException方法中打印日志即可

public class RuntimeExceptionHandle implements Thread.UncaughtExceptionHandler {
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        //打印异常信息到日志
        System.out.println("异常处理器调用, 打印日志: " + e);
    }
}

放开上述代码注释,执行结果:

主线程执行结束!!!
任务开始执行
异常处理器调用, 打印日志: java.lang.ArithmeticException: / by zero

子线程在抛出运行时异常,调用自定义的异常处理器,进行异常处理(日志打印)

原理分析:

从JDK1.5开始,当一个线程因未捕获的异常而即将终止时,JAVA虚拟机将使用Thread.getUncaughtExceptionHandler()查询该线程以获得其UncaughtExceptionHandler,并调用该handler的uncaughtException()方法,将线程和异常作为参数传递。如果没有,则搜索该线程的ThreadGroup的异常处理器。

ThreadGroup中的默认异常处理器实现是将处理工作逐层委托给上层的ThreadGroup,直到某个ThreadGroup的异常处理器能够处理该异常,否则一直传递到顶层的ThreadGroup。顶层ThreadGroup的异常处理器委托给默认的系统处理器(如果默认的处理器存在,默认情况下为空),否则把栈信息输出到System.err

二、线程池异常

线程池中的异常如果处理不好的话,经常会出现日志不打印,导致不能定位问题

public class ThreadPoolExecption {
    private static ExecutorService executor = Executors.newFixedThreadPool(3);

    public static void main(String[] args) {
        Task task = new Task();
        try {
            executor.execute(task);
            //executor.submit(task);
        } catch (Exception e) {
            System.out.println("捕获线程池的线程异常");
        }
    }
}

运行结果:

任务开始执行
Exception in thread “pool-1-thread-1” java.lang.ArithmeticException: / by zero
at com.ljj.threadException.Task.run(Task.java:30)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

虽然在控制台打印了异常堆栈信息,但是我们外层的catch并没有捕获这个运行时异常,在生产环境中,异常是不可能直接打印在控制台的,而是打印到对应的日志文件中去。

注意线程池执行有两个方法,如果使用submit方法的话,堆栈信息在控制台也不会打印。

分析线程池源码:

public void execute(Runnable command) {
    if (command == null)
        throw new NullPointerException();
    
    int c = ctl.get();
    if (workerCountOf(c) < corePoolSize) {
        if (addWorker(command, true))
            return;
        c = ctl.get();
    }
    if (isRunning(c) && workQueue.offer(command)) {
        int recheck = ctl.get();
        if (! isRunning(recheck) && remove(command))
            reject(command);
        else if (workerCountOf(recheck) == 0)
            addWorker(null, false);
    }
    else if (!addWorker(command, false))
        reject(command);
}
/**
* addWorker方法部分内容
*/
w = new Worker(firstTask); //封装成Worker对象
final Thread t = w.thread;
if (t != null) {
    final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();
    try {
        // Recheck while holding lock.
        // Back out on ThreadFactory failure or if
        // shut down before lock acquired.
        int rs = runStateOf(ctl.get());

        if (rs < SHUTDOWN ||
            (rs == SHUTDOWN && firstTask == null)) {
            if (t.isAlive()) // precheck that t is startable
                throw new IllegalThreadStateException();
            workers.add(w);
            int s = workers.size();
            if (s > largestPoolSize)
                largestPoolSize = s;
            workerAdded = true;
        }
    } finally {
        mainLock.unlock();
    }
    if (workerAdded) {
        t.start();
        workerStarted = true;
    }

/**
* worker对象里面的run方法部分内容
*/
while (task != null || (task = getTask()) != null) {
    w.lock();
     if ((runStateAtLeast(ctl.get(), STOP) ||
          (Thread.interrupted() &&
           runStateAtLeast(ctl.get(), STOP))) &&
         !wt.isInterrupted())
         wt.interrupt();
     try {
         beforeExecute(wt, task);
         Throwable thrown = null;
         try {
             task.run();
         } catch (RuntimeException x) {
             thrown = x; throw x;
         } catch (Error x) {
             thrown = x; throw x;
         } catch (Throwable x) {
             thrown = x; throw new Error(x);
         } finally {
             afterExecute(task, thrown); //执行改方法
         }
     } finally {
         task = null;
         w.completedTasks++;
         w.unlock();
     }
 }

首先ThreadPoolExecutor中execute方法会将传入的task封装成Worker对象,在进入Worker对象的run方法,发现异常被线程池捕获了,但是最后在finally会执行 afterExecute(task, thrown)方法,该方法的方法体是空,里面没有任何逻辑。

三、线程池异常处理方法

1. run方法里面try/catch所有处理逻辑
public void run() {
try {
	//处理逻辑
   } catch(Exeception e) {
      //打印日志
	}
}

这是一种简单而且不易出错的线程池异常处理方式,推荐使用

2. 自定义异常处理器

由于线程池传入的参数是Runnable不是Thread,执行一个个对应的任务,所以这里我们需要使用ThreadFactory创建线程池

public class MyThreadFactory implements ThreadFactory {
    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(r);
        t.setUncaughtExceptionHandler(new RuntimeExceptionHandle());
        return t;
    }
}

继承ThreadFactory,并重写newThread(Runnable r)方法设置异常处理器,在异常处理器中捕获并处理异常(打印日志)

public class ThreadPoolExecption1 {
    private static ExecutorService executor = Executors.newSingleThreadExecutor(new MyThreadFactory());

    public static void main(String[] args) {
        Task task = new Task();
        executor.execute(task);
        //executor.submit(task);
    }
}

执行结果:

任务开始执行
异常处理器调用, 打印日志: java.lang.ArithmeticException: / by zero

这种方法比较麻烦,更简单的是在Thread类中设置一个静态域,并将这个处理器设置为默认异常处理器

public class ThreadPoolExeception2 {
    private static ExecutorService executor = Executors.newFixedThreadPool(3);

    public static void main(String[] args) {
        Thread.setDefaultUncaughtExceptionHandler(new RuntimeExceptionHandle());
        Task task = new Task();
        executor.execute(task);
        //executor.submit(task);
    }
}

注意上述方法只有在执行execute方法才可以捕获异常进行处理,submit方法是不起作用的,没有任何异常信息输出。

3. 重写ThreadPoolExecutor.afterExecute方法

前面分析过,线程池的线程在执行结束前肯定调用afterExecute方法,所有只需要重写该方法即可。

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

    @Override
    public void afterExecute(Runnable r, Throwable t) {
        if(t != null) {
            System.out.println("打印异常日志:" + t);
        }
    }
}

继承ThreadPoolExecutor 类并重写afterExecute()方法

public class ThreadPoolExeception4 {
    private static ThreadPoolExecutor executor = new MyThreadPool(1, 1, 0,
            TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());

    public static void main(String[] args) {
        Task task = new Task();
        executor.execute(task);
        //executor.submit(task);
        executor.shutdown();
    }
}

执行结果:

任务开始执行
Exception in thread “pool-1-thread-1” 打印异常日志:java.lang.ArithmeticException: / by zero
java.lang.ArithmeticException: / by zero
at com.ljj.threadException.Task.run(Task.java:30)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

同样,这种方式submit方法是不适用的

4. 使用submit执行任务
public class ThreadPoolExeception3 {
    private static ExecutorService executor = Executors.newFixedThreadPool(3);

    public static void main(String[] args) {
        Task task = new Task();
        try {
            Future future = executor.submit(task);
            System.out.println(future.get());
        } catch (Exception e) {
            System.out.println("捕获线程池的线程异常:" + e);
        }
    }
}

执行结果:

任务开始执行
捕获线程池的线程异常:java.util.concurrent.ExecutionException: java.lang.ArithmeticException: / by zero

可以看到在使用submit执行任务,该方法将返回一个Future对象,不仅仅是任务的执行结果,异常也会被封装到Future对象中,通过get()方法获取。

源码分析

public Future<?> submit(Runnable task) {
        if (task == null) throw new NullPointerException();
        RunnableFuture<Void> ftask = newTaskFor(task, null);
        execute(ftask); //封装成FutureTask对象交给execute方法
        return ftask;
    }

由于调用ThreadPoolExecutor的execute方法,会被封装成Worker对象,然后调用FutureTask对象的run方法:

public void run() {
	if (state != NEW ||
	     !UNSAFE.compareAndSwapObject(this, runnerOffset,
	                                  null, Thread.currentThread()))
	     return;
	 try {
	     Callable<V> c = callable;
	     if (c != null && state == NEW) {
	         V result;
	         boolean ran;
	         try {
	             result = c.call();
	             ran = true;
	         } catch (Throwable ex) {
	             result = null;
	             ran = false;
	             setException(ex);  //捕获异常
	         }
	         if (ran)
	             set(result);
	     }
	 } finally 

捕获异常后调用setException(ex)方法,setExcetion首先是将一个异常信息赋值给一个全局变量outcome,并且将全局的任务状态state字段通过CAS更新为3(异常状态)然后最后做一些清理工作

FutureTask.get()方法中,会对setException方法中设置的outcome和state做一些逻辑判断,然后直接往上抛出了异常,所以我们就可以在主线程中捕获这个异常

public V get() throws InterruptedException, ExecutionException {
   int s = state;
     if (s <= COMPLETING)
         s = awaitDone(false, 0L);
     return report(s);
 }


private V report(int s) throws ExecutionException {
   Object x = outcome;
     if (s == NORMAL)
         return (V)x;
     if (s >= CANCELLED)
         throw new CancellationException();
     throw new ExecutionException((Throwable)x);
 }
  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++中,线程池异常处理主要是通过捕获和处理线程中抛出的异常来实现的。以下是一个简单的示例,展示了如何在C++线程池中使用异常处理: ```cpp #include <iostream> #include <thread> #include <vector> #include <functional> #include <stdexcept> #include <future> #include <mutex> #include <condition_variable> #include <queue> class ThreadPool { public: ThreadPool(size_t); template<class F, class... Args> auto enqueue(F&& f, Args&&... args) -> std::future<typename std::result_of<F(Args...)>::type>; ~ThreadPool(); private: // need to keep track of threads so we can join them std::vector< std::thread > workers; // the task queue std::queue< std::function<void()> > tasks; // synchronization std::mutex queue_mutex; std::condition_variable condition; bool stop; }; // the constructor just launches some amount of workers inline ThreadPool::ThreadPool(size_t threads) : stop(false) { for(size_t i = 0;i<threads;++i) workers.emplace_back( [this] { for(;;) { std::function<void()> task; { std::unique_lock<std::mutex> lock(this->queue_mutex); this->condition.wait(lock, [this]{ return this->stop || !this->tasks.empty(); }); if(this->stop && this->tasks.empty()) return; task = std::move(this->tasks.front()); this->tasks.pop(); } try { task(); } catch(const std::exception& e) { // handle exception in task std::cerr << "Exception in thread: " << e.what() << "\n"; } catch(...) { // unknown exception in task std::cerr << "Unknown exception in thread\n"; } } } ); } // add new work item to the pool template<class F, class... Args> auto ThreadPool::enqueue(F&& f, Args&&... args) -> std::future<typename std::result_of<F(Args...)>::type> { using return_type = typename std::result_of<F(Args...)>::type; auto task = std::make_shared< std::packaged_task<return_type()> >( std::bind(std::forward<F>(f), std::forward<Args>(args)...) ); std::future<return_type> res = task->get_future(); { std::unique_lock<std::mutex> lock(queue_mutex); // don't allow enqueueing after stopping the pool if(stop) throw std::runtime_error("enqueue on stopped ThreadPool"); tasks.emplace([task](){ (*task)(); }); } condition.notify_one(); return res; } // the destructor joins all threads inline ThreadPool::~ThreadPool() { { std::unique_lock<std::mutex> lock(queue_mutex); stop = true; } condition.notify_all(); for(std::thread &worker: workers) worker.join(); } ``` 在这个示例中,我们创建了一个`ThreadPool`类,它有一个任务队列和一个工作线程列表。当我们向线程池提交一个任务时,这个任务会被添加到任务队列中。工作线程会从队列中取出任务并执行。如果任务中的代码抛出了异常,我们可以在任务中捕获并处理这个异常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值