ThreadPoolExecutor异常处理

时间:2024-11-28 14:36:31

java.util.concurrent包中的ThreadPoolExecutor,提供了java语言的线程池,你可以提交一个返回结果的任务(submit(Callable),返回Future),或者执行一个不返回结果的任务(execute(Runnable)),但提交的任务可能会抛异常,这就需要处理异常:

1. 对于submit的任务,框架会将异常保持在future里,并包装在ExecutionException里,当调用Future.get()时,再次throw,这时可以调用ExecutionException.getCause()获取包装的exception,这种情况下,设置UncaughtExceptionHandler也不会被调用。

2. 对应execute的任务,会直接throw,可以设置一个UncaughtExceptionHandler,例如:

  1. import java.lang.Thread.UncaughtExceptionHandler;
  2. import java.util.Random;
  3. import java.util.concurrent.CancellationException;
  4. import java.util.concurrent.ExecutionException;
  5. import java.util.concurrent.ExecutorService;
  6. import java.util.concurrent.Future;
  7. import java.util.concurrent.LinkedBlockingQueue;
  8. import java.util.concurrent.ThreadFactory;
  9. import java.util.concurrent.ThreadPoolExecutor;
  10. import java.util.concurrent.TimeUnit;
  11. import java.util.concurrent.atomic.AtomicInteger;
  12. public class ExecutorServiceDemo {
  13. public static void testExecute() {
  14. ExecutorService executors = new ThreadPoolExecutor(10, 20, 60, TimeUnit.SECONDS,
  15. new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {
  16. final AtomicInteger threadNumber = new AtomicInteger(1);
  17. public Thread newThread(Runnable r) {
  18. Thread t = new Thread(Thread.currentThread().getThreadGroup(), r, "topPatternTasklet-thread"
  19. + (threadNumber.getAndIncrement()));
  20. t.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
  21. public void uncaughtException(Thread t, Throwable e) {
  22. System.out.println(e);
  23. }
  24. });
  25. return t;
  26. }
  27. }, new ThreadPoolExecutor.CallerRunsPolicy());
  28. final Random r = new Random();
  29. for (int i = 0; i < 10; ++i) {
  30. executors.execute(new Runnable() {
  31. public void run() {
  32. try {
  33. int ri = r.nextInt(1000);
  34. Thread.sleep(ri);
  35. if (ri % 3 == 0) {
  36. System.out.println("ri:" + ri);
  37. throw new RuntimeException("haha error!");
  38. }
  39. System.out.println(Thread.currentThread());
  40. } catch (InterruptedException e) {
  41. }
  42. }
  43. });
  44. }
  45. executors.shutdown();
  46. System.out.println("finished");
  47. }
  48. public static void main(String[] args) {
  49. testExecute();
  50. }
  51. }