V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
JingSmith
V2EX  ›  程序员

CompletableFuture 的 Signaller 实现是不是有并发问题?

  •  
  •   JingSmith · 42 天前 · 701 次点击
    这是一个创建于 42 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我看 CompletableFuture 的 Signaller 实现是不是有并发问题,会导致线程永久阻塞?具体代码是 tryFire 方法中的 LockSupport.unpark(w); 在 block 方法的 while (!isReleasable()) { 之后 LockSupport.park(this); 之前执行,线程就永久阻塞了。

    我看 GPT 也认同了我的观点,这是我理解错了还是他真有 bug ?

    2 条回复    2024-03-17 22:19:51 +08:00
    JingSmith
        1
    JingSmith  
    OP
       41 天前
    没 bug ,先 unpark 的话,下次 park 会失效,应该是有个标志位,能抵消一次 park

    ```java
    @Test
    public void test() throws InterruptedException {


    Thread thread = new Thread(() -> {
    try {
    TimeUnit.SECONDS.sleep(3);
    } catch (InterruptedException e) {
    throw new RuntimeException(e);
    }
    System.out.println("start park in thread 1");
    LockSupport.park(Thread.currentThread());
    System.out.println("start park in thread 2");
    LockSupport.park(Thread.currentThread());
    System.out.println("end park in thread");
    });

    thread.start();

    TimeUnit.SECONDS.sleep(1);

    System.out.println("start unpark in main");
    LockSupport.unpark(thread);
    LockSupport.unpark(thread);
    System.out.println("end unpark in main");

    TimeUnit.SECONDS.sleep(4);

    LockSupport.unpark(thread);

    TimeUnit.SECONDS.sleep(100);

    }
    ```
    yeqizhang
        2
    yeqizhang  
       40 天前
    学习了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2982 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 13:28 · PVG 21:28 · LAX 06:28 · JFK 09:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.