线程池中处理异常
在传递任务中去处理异常
class Task implements Runnable{
@Override
public void run() {
System.out.println("task start...");
try {
int i = 1 / 0;
} catch (Exception e) {
System.err.println( e.getCause().getMessage());
}
System.out.println("task end...");
}
}
public class MyTest {
@Test
public void testSubmit() {
ExecutorService executorService = Executors.newFixedThreadPool(1);
executorService.execute(new Task()); // 因为在线程实现类中处理了,这里没有异常
executorService.shutdown();
}
}
Future的get方法中处理异常
- execute是Executor接口的方法,submit是ExecuteService接口的方法。
- execute的入参是Runnable,submit的入参可以是Runnable、Callable、Runnable和一个返回值。
- execute没有返回值,submit有返回值。
- 方法中抛出异常,execute会直接抛出异常,submit会在获取结果的时候抛出异常,如果不获取结果,submit不抛出异常。
示例
class Task implements Runnable{
@Override
public void run() {
System.out.println("task start...");
int i = 1 / 0;
System.out.println("task end...");
}
}
public class MyTest {
@Test
public void testSubmit() {
ExecutorService executorService = Executors.newFixedThreadPool(1);
executorService.submit(new Task()); // 不会抛出异常
executorService.shutdown();
}
@Test
public void testExecute() {
ExecutorService executorService = Executors.newFixedThreadPool(1);
executorService.execute(new Task()); // 会抛出异常, 但这里try-catch不会捕获到异常
executorService.shutdown();
}
@Test
public void testGet() {
// 下面测试使用Future的get方法获取结果
ExecutorService executorService = Executors.newFixedThreadPool(1);
Future future = executorService.submit(new Task());
try {
future.get(); // submit方法时只有在使用Future的get方法时才会抛出异常
} catch (Exception e) {
System.err.println(e.getCause().getMessage());
}
executorService.shutdown();
}
}
自定义工厂中设置异常处理器
class Task implements Runnable{
@Override
public void run() {
System.out.println("task start...");
int i = 1 / 0;
System.out.println("task end...");
}
}
class MyFactory implements ThreadFactory {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
// 设置异常处理器
t.setUncaughtExceptionHandler((t1, e) -> {
System.err.println( e.getCause().getMessage());
});
return t;
}
}
public class MyTest {
@Test
public void testFactory() {
ExecutorService executorService = Executors.newFixedThreadPool(1,new MyFactory());
executorService.execute(new Task()); // 在工厂中设置了异常处理器,所以不会抛出异常
executorService.shutdown();
}
}
在任务执行后添加钩子函数
class Task implements Runnable{
@Override
public void run() {
System.out.println("task start...");
int i = 1 / 0;
System.out.println("task end...");
}
}
public class MyTest {
@Test
public void testFactory() {
ExecutorService executorService = Executors.newFixedThreadPool(1,new MyFactory());
executorService.execute(new Task()); // 在工厂中设置了异常处理器,所以不会抛出异常
executorService.shutdown();
}
@Test
public void testAfterExecute() {
ExecutorService executorService = new ThreadPoolExecutor(
2,
3,
0,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(10)
) {
// 重写afterExecute方法, 统一处理线程池里抛出的异常
@Override
protected void afterExecute(Runnable r, Throwable t) {
// 这个是execute提交的时候
if (t != null) {
System.err.println( t.getMessage());
}
// submit提交的时候
if (r instanceof FutureTask) {
try {
Future<?> future = (Future<?>) r;
// get获取异常
future.get();
} catch (Exception e) {
System.err.println(e.getCause().getMessage());
}
}
}
};
executorService.execute(new Task());
executorService.submit(new Task());
executorService.shutdown();
}
}
「点点赞赏,手留余香」
还没有人赞赏,快来当第一个赞赏的人吧!
给Admin打赏
×
予人玫瑰,手有余香
- 2¥
- 5¥
- 10¥
- 20¥
- 50¥
¥2
声明:本文为原创文章,版权归信息岛所有,欢迎分享本文,转载请保留出处!