V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
JohnZorn
V2EX  ›  Java

异步日志打印

  •  
  •   JohnZorn · Dec 20, 2018 · 3338 views
    This topic created in 2686 days ago, the information mentioned may be changed or developed.

    日志打印使用 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。

    7 replies    2018-12-20 13:56:06 +08:00
    JohnZorn
        1
    JohnZorn  
    OP
       Dec 20, 2018
    题目描述错了(试了一下没出现我说的那种情况),说实际情况吧。
    昨天看到有个老哥发的 jedis bug 然后下面说出了 lettuce
    今天就说试一下 lettuce
    创建的 RedisAsyncCommands
    然后 log.info( asyncCommands.get("key").get() ); 这种打印不出来。 可能是 lettuce 这个异步命令的事吧
    lovedebug
        3
    lovedebug  
       Dec 20, 2018
    异步调用没返回你怎么打印结果
    JohnZorn
        4
    JohnZorn  
    OP
       Dec 20, 2018
    @lovedebug 你有没有写代码试一下
    JohnZorn
        5
    JohnZorn  
    OP
       Dec 20, 2018
    平常 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 处断点卡上一下的话。就可以打印了
    JohnZorn
        6
    JohnZorn  
    OP
       Dec 20, 2018
    是因为主线程结束太快了
    lihongjie0209
        7
    lihongjie0209  
       Dec 20, 2018
    你的 future 结束了吗?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1439 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 16:56 · PVG 00:56 · LAX 09:56 · JFK 12:56
    ♥ Do have faith in what you're doing.