gpt4 book ai didi

java - 为什么 java TransferQueue 在 "transfer()"之后不能 "put()"?

转载 作者:行者123 更新时间:2023-11-29 04:12:47 24 4
gpt4 key购买 nike

我有一个非常简单的代码片段:

public static void main(String [] args) throws InterruptedException {
TransferQueue<Integer> transferQueue = new LinkedTransferQueue<>();
System.out.println("Step1");
transferQueue.put(2);
System.out.println("Step2");
transferQueue.transfer(2);
System.out.println("Step3");
}

运行这个程序,它打印:

Step1
Step2

然后卡在那里。那么为什么“transfer()”函数不起作用?

最佳答案

这由 javadoc 解释对于 transfer(e) 方法。

More precisely, transfers the specified element immediately if there exists a consumer already waiting to receive it (in take() or timed poll), else inserts the specified element at the tail of this queue and waits until the element is received by a consumer.

强调已添加!

在您的示例中,没有消费者接收元素,因此 transfer(2) 调用永远阻塞。

(这与前面的 put(2) 调用无关。)

关于java - 为什么 java TransferQueue 在 "transfer()"之后不能 "put()"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54086212/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com