CompletableFuture.supplyAsync多线程异常捕获
/** * 保存错误信息 */ public volatile RuntimeException error = null; /** * 自动发起流程 * synchronized 保证对error的修改不会被其他因素影响 */ @Override public synchronized void autoMakeProcess() { error = null; CompletableFuture.supplyAsync(() -> { long t1 = System.currentTimeMillis(); log.info("自动发起流程任务开始"); safetyComplianceProcessAutoService.plmNewItemStartProcess(); safetyComplianceProcessAutoService.checkTimeOutStartProcess(); log.info("自动发起流程任务结束,耗时:{}ms", (System.currentTimeMillis() - t1)); return true; }).exceptionally( e -> { if (e != null) { log.error("autoMakeProcess error:", e); error = new RuntimeException(e); } return null; }); if (!ForkJoinPool.commonPool().awaitQuiescence(1, TimeUnit.HOURS)) { log.error("autoMakeProcess error: 任务执行超时"); } //如果有异常 抛出异常 if(error != null){ throw error; } }
标签:多线程,log,CompletableFuture,error,null,supplyAsync From: https://www.cnblogs.com/yexuba/p/17119422.html