线程池异常怎么处理知道吗?
在使用线程池处理任务的时候,任务代码可能抛出RuntimeException,抛出异常后,线程池可能捕获它,也可能创建一个新的线程来代替异常的线程,我们可能无法感知任务出现了异常,因此我们需要考虑线程池异常情况。
常见的异常处理方式:
1.try-catch捕获异常
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author 百里
*/
public class BaiLiHandlerException implements Runnable {
@Override
public void run() {
try {
// 任务代码
int a = 10 / 0;
} catch (Exception e) {
System.err.println("任务执行异常:" + e.getMessage());
}
}
public static void main(string[] args) {
ExecutorService executor = Executors.newFixedThreadPool(10);
BaiLiHandlerException task = new BaiLiHandlerException();
executor.execute(task);
}
}
2.使用Thread.UncaughtExceptionHandler处理异常
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.*;
/**
* @author 百里
*/
public class BaiLiHandlerException implements Runnable {
@Override
public void run() {
// 任务代码
int a = 10 / 0;
}
public static class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.err.println("任务执行异常:" + e.getMessage());
}
}
public static void main(String[] args) {
BaiLiHandlerException task = new BaiLiHandlerException();
Thread thread = new Thread(task);
thread.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
thread.start();
}
}
3.重写ThreadPoolExecutor.afterExcute处理异常
package exception;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.*;
/**
* @author 百里
*/
public class BaiLiHandlerException implements Runnable {
@Override
public void run() {
// 任务代码
int a = 10 / 0;
}
public static class MyThreadPoolExecutor extends ThreadPoolExecutor {
public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if (t != null) {
System.err.println("任务执行异常:" + t.getMessage());
}
}
}
public static void main(String[] args) {
MyThreadPoolExecutor executor = new MyThreadPoolExecutor(1, 1, 0, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(), new ThreadFactoryBuilder().setNameFormat("MyThread-%d").build());
BaiLiHandlerException task = new BaiLiHandlerException();
executor.execute(task);
}
}
4.使用future.get处理异常
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.*;
/**
* @author 百里
*/
public class BaiLiHandlerException {
public static void main(String[] args) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<String> future = executor.submit(() -> {
throw new RuntimeException("任务执行失败");
});
try {
String result = future.get();
System.out.println(result);
} catch (ExecutionException e) {
Throwable ex = e.getCause();
System.out.println("捕获到异常: " + ex.getMessage());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
executor.shutdownNow();
System.out.println("线程被中断,已执行相应处理");
}
executor.shutdown();
}
}