zhangLLL
V2EX  ›  Java

问一个 ExecutorService 的问题

  •  
  •   zhangLLL · Sep 29, 2021 · 2618 views
    This topic created in 1860 days ago, the information mentioned may be changed or developed.

    想知道 ExecutorService 提交的任务先 sleep1 秒是何意义?

        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(() -> {
            try {
                Thread.sleep(1000);
    
                doSomething();
    
            } catch (Exception e) {
                logger.error("异步接口执行失败", e);
            }
        });
    
    8 replies  •  2021-10-01 01:23:08 +08:00
    billlee
    billlee
    Sep 29, 2021
    1
    为以后的优化留下空间?
    SoloCompany
    SoloCompany
    Sep 29, 2021 via iPhone
    2
    拿线程当协程用?先把写这个代码的人找出来打一顿吧
    wangyu17455
    wangyu17455
    Sep 30, 2021
    3
    理解不能
    Kamiyu0087
    Kamiyu0087
    Sep 30, 2021
    4
    可能是为了另一个任务优先执行?
    不过这么写肯定是错的
    ikas
    ikas
    Sep 30, 2021
    5
    没有上下文..无法知道干嘛的...
    cnzjl
    cnzjl
    Sep 30, 2021
    6
    等上一个事务运行完毕(
    BanGanExpert
    BanGanExpert
    Sep 30, 2021
    7
    newSingleThreadExecutor(),说白了就是先异步执行 doSomething()这部分代码,因为这里明确只用了一个线程,然后线程里面先等个 1 秒,
    这玩意说明他就是想主线程的执行完撒操作在处理,理论上 1 秒只要不做什么访问数据库或者远程接口的东西基本很多事情已经完成了,不过这么写是要被打的,哈哈哈
    cheng6563
    cheng6563
    Oct 1, 2021
    8
    当 defer 用呗。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1368 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 22:10 · PVG 06:10 · LAX 14:10 · JFK 17:10
    ♥ Do have faith in what you're doing.