yanshenxian
V2EX  ›  Java

kafka 时间轮 tick 推进 为什么不用 delayQueue#take

  •  
  •   yanshenxian · Jul 20, 2020 · 2276 views
    This topic created in 2148 days ago, the information mentioned may be changed or developed.

    图片方框中的代码使用的是 bucket = delayQueue.poll(timeoutMs, TimeUnit.MILLISECONDS);

    27C51040-C4DB-4965-92A5-C0667F2BCEF3.jpeg

    如果第一个即将到期的任务 >> 轮询的 timeoutMs, 这样不是会造成大量 cpu 空转吗

    为什么不能用 bucket = delayQueue.take();

    6 replies    2020-07-21 10:14:17 +08:00
    mightofcode
        2
    mightofcode  
       Jul 20, 2020
    take 不能指定超时
    yanshenxian
        3
    yanshenxian  
    OP
       Jul 20, 2020
    @mightofcode 这个不需要指定超时啊 时间轮只需要把过期的 task 取出来就行
    mightofcode
        4
    mightofcode  
       Jul 20, 2020
    @yanshenxian 不指定超时的话不就一直阻塞在这里了吗,看情况应该是不想阻塞在这吧
    yanshenxian
        5
    yanshenxian  
    OP
       Jul 20, 2020
    @mightofcode 嗯 我觉得这里一直阻塞到有任务过期也是没问题的,换成 poll 也是相当于循环阻塞知道有任务过期
    hfc
        6
    hfc  
       Jul 21, 2020
    没有 CPU 空转吧,这里的 timeoutMs 时间内线程是 TIMEOUT_WATING 状态,不会占用 CPU 。超时没有获取到任务就返回 false 了。
    至于为什么用 take,大概就是需要一个超时能自动结束的接口,翻翻代码或许也能找到使用 take 的接口,但是两者使用场景不同。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2820 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 12:17 · PVG 20:17 · LAX 05:17 · JFK 08:17
    ♥ Do have faith in what you're doing.