java - 如何等待队列包含元素?

标签 java queue wait

我正在尝试实现一个 FIFO 观察器/可观察解耦队列,但我不确定如何获得一种方法来等待队列不为空后再返回。这是我目前的尝试,但我相信一定有更优雅的解决方案。

/*
 * Waits until there is data, then returns it.
 */
private Double[] get() {
    while (queue.isEmpty()) {
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            // Don't care.
        }
    }

    return queue.removeFirst();
}

最佳答案

为什么不使用 BlockingQueue - 这正是您想要的。

关于java - 如何等待队列包含元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10258604/

相关文章:

java - PDFLib 在 PHP 中获取图层名称和句柄

c - 线程无法正常工作 - C

javascript - 如何在 JavaScript 中将整个脚本暂停一段时间?这可能吗?

java - 为什么 BasicService.getCodeBase() 在使用文件 ://as URL protocol in the JNLP codebase attribute? 时返回 null

java - 如何解决==> org.hibernate.exception.GenericJDBCException : Could not open connection

linux - 如何在 Linux 中查找处理器队列长度

javascript - 检查ServiceWorker是否处于等待状态

android - 同步 startActivityForResult - 等待 Activity 完成

java - jsp中不显示值

javascript - jQuery stop(true, true) 跳转到队列中所有动画的末尾