V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
JingSmith
17.36D
V2EX  ›  程序员

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

  •  
  •   JingSmith · Mar 16, 2024 · 1469 views
    This topic created in 784 days ago, the information mentioned may be changed or developed.

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

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

    2 replies    2024-03-17 22:19:51 +08:00
    JingSmith
        1
    JingSmith  
    OP
       Mar 16, 2024
    没 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);

    }
    ```
    yungo8
        2
    yungo8  
       Mar 17, 2024
    学习了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1061 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 18:36 · PVG 02:36 · LAX 11:36 · JFK 14:36
    ♥ Do have faith in what you're doing.