日志打印使用 Future 和主线程是什么关系的 如果 future = executorService.submit(new Callable()); s = future.get(); log.info(s); 这绝对是没事的。 如果写在内部调用 log.info(executorService.submit(new Callable()).get());
或者 log.info(executorService.submit(new Callable()).get() == null ? "null" : "not null"); 这也是不行的 打印不出来 那么如果直接 log 输出方法内部调用异步方法的话 log.info()方法看到的是什么? 是无参的 Log.info()吗?
因为如果断点卡住主线程的话,卡一下就能打印出来。
logback。
1 choice4 OP 题目描述错了(试了一下没出现我说的那种情况),说实际情况吧。 昨天看到有个老哥发的 jedis bug 然后下面说出了 lettuce 今天就说试一下 lettuce 创建的 RedisAsyncCommands 然后 log.info( asyncCommands.get("key").get() ); 这种打印不出来。 可能是 lettuce 这个异步命令的事吧 |
![]() | 2 0915240 2018-12-20 13:23:10 +08:00 |
![]() | 3 lovedebug 2018-12-20 13:24:27 +08:00 异步调用没返回你怎么打印结果 |
5 choice4 OP 平常 jdk 的调用方式 ExecutorService service = Executors.newFixedThreadPool(1); log.error(service.submit(() -> { Thread.sleep(30000); return 100; }).get().toString()); service.shutdown(); 这样可以在 get()方法处阻塞获取值 ,最后成功打印。 但是 RedisClient redisClient = RedisClient.create("redis://127.0.0.1:6379"); StatefulRedisConnection<String, String> redisCOnnection= redisClient.connect(); RedisAsyncCommands<String, String> asyncCommands = redisConnection.async(); RedisFuture<String> future = asyncCommands.get("lettuce_key"); future.whenCompleteAsync((s, throwable) -> { try { log.error(asyncCommands.get("lettuce_key").get()); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } }); 这样是空的, 如果在 future.whenCompleteAsync 处断点卡上一下的话。就可以打印了 |
6 choice4 OP 是因为主线程结束太快了 |
7 lihongjie0209 2018-12-20 13:56:06 +08:00 你的 future 结束了吗? |