- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
如果我只是做这样的事情:
synchronized(taskQueue) { //taskQueue is a BlockingQueue
taskQueue.drainTo(tasks); //tasks is a list
}
我确定不能在同步块(synchronized block)内执行对 taskQueue.put()
和 taskQueue.take()
的并发调用吗?
换句话说,我是否使 drainTo() 方法成为原子方法?
或者更一般地说,我如何使线程安全操作的组合成为原子?
例子:
if(taskQueue.size() == 1) {
/*Do a lot of things here, but I do not want other threads
to change the size of the queue here with take or put*/
}
//taskQueue.size() must still be equal to 1
最佳答案
请参阅以下摘录自 Java docs of BlockingQueue
BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control. However, the bulk Collection operations addAll, containsAll, retainAll and removeAll are not necessarily performed atomically unless specified otherwise in an implementation. So it is possible, for example, for addAll(c) to fail (throwing an exception) after adding only some of the elements in c.
此外,请查看示例,该示例显示 BlockingQueue 实现可以安全地用于多个生产者和多个消费者。
因此,如果您没有使用 addAll、containsAll、retainAll 和 removeAll
等批量收集操作,那么您就是线程安全的。
您甚至不需要synchronized(taskQueue) {
,可以直接使用taskQueue.drainTo(tasks);
,因为BlockingQueue 实现是线程安全的 用于非批量收集操作,如 put
、take
、drainTo
等
希望这对您有所帮助!
关于java - 是否同步到足以使 BlockingQueue 的 drainTo() 方法原子化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30964375/
我的理解是1.5加入了callable,runnable接口(interface)保持原样,防止世界末日。为什么我不能实例化 ThreadPoolExecutor (core, max, tu, un
我正在制作一个包含两个线程的应用程序:其中一个向 LinkedBlockingQueue 写入一个值,另一个正在读取。我正在使用 ScheduledExecutorService 在某个时间段内以秒为
我正在尝试 Autowiring 参数化的阻塞队列: @Bean(name = "saveProductQueue") public BlockingQueue saveProductQueue()
我正在实现一个程序,其中主线程将各种消息推送到工作线程,而工作线程将工作结果推送回主线程。 为此,我计划使用两个队列,一个用于推送到工作线程,另一个用于从中拉出。 据我了解,线程会缓存对象,因此如果它
我正在使用 RabbitMQ,它默认为消费者使用 LinkedBlockingQueue 。它有一个阻塞的 nextDelivery() 方法,该方法基本上调用队列上的 take() 。 但是如果在调
我有 n 个生产者线程通过 BlockingQueue 为 1 个消费者线程提供数据。我正在使用 .put 和 .take (后者当 .peek != null 时)。除了明显在传输过程中不可避免的数
我已经实现了一个带有套接字线程池的两人游戏。每个玩家都连接到自己的线程。我按照this添加了一个消息队列系统文章。 问题是消息滞后。第一个玩家的第一个响应会按预期添加到 messageQueue 中。
据我所知,BlockingCollection 使用非繁忙等待,这是对新项目/回调的通知。所以我不明白它是如何阻塞的,但我认为我可能混合了阻塞的线程和阻塞的共享对象访问? 最佳答案 这是一个很好的解释
我的 Android 应用程序有一个长时间运行的后台服务,据我所知,它在应用程序的主线程中运行,因此,任何耗时或阻塞的任务都应移至单独的线程。 现在,情况是这样的,我不明白/困惑: 当我从一个 Act
我在数据库前面使用 LinkedBlockingQueue。一个线程写入队列,另一个线程从队列读取。 我认为两个并发写入是不可能的。但是是否有可能一个线程写入而另一个线程同时从队列中读取呢?如果没有,
我有一个在单个后台线程上处理工作事件的 BlockingQueue。各种线程调用 add 将一些工作添加到队列中,单个后台线程调用 take 获取工作并一次处理一个。最终可能是停止处理工作的时候了,我
我在多线程系统中使用 BlockingQueue,其中同步块(synchronized block)将项目添加到列表中。有时它不会将项目添加到列表中,它遗漏的项目是随机的。我尝试将以下行添加到代码中,
我有以下阻塞队列; final BlockingQueue blockingQueue = new LinkedBlockingQueue(); 在哪里 public class Message
这个问题的标题让我怀疑这是否存在,但仍然: 我感兴趣的是是否有 Java 的 BlockingQueue 的实现,它受大小限制,从不阻塞,而是在尝试入队太多元素时抛出异常。 编辑 - 我将 Block
假设我有 BlockingQueue 并且一些线程被称为 take() 但此时队列是空的。假设我以某种方式知道将来不会有新元素出现在队列中。如何释放那些被称为 take() 的线程等待?谢谢! p
我想要一个线程安全的容器,它会阻止调用者,直到有项目可用为止。项目将以每秒 1000 秒的速度添加到此容器中,但不会以相同的速度排出。因此,我希望容器不允许重复。我围绕 LinkedBlockingQ
我正在使用 BlockingQueue(LinkedBlockingQueue) 在多个线程之间同步数据。请看下图。 主线程是一个生产者,它生产对象,然后将它们放入每个消费者的队列中(线程2-10)。
我有一个容量为 1 的 BlockingQueue。它存储收到的股票的最后价格。价格保留在队列中,直到客户端轮询队列。然后,我有一个名为 getLatestPrice() 的方法,它应该返回股票的最新
我的 Java 代码中使用 BlockingQueue 时存在潜在的竞争条件,我想知道如何修改代码来避免这种情况: private static BlockingQueue ftpQueue = ne
我有一种情况需要 BlockingQueue 上的方法 peekWait。我会将此方法描述为 retrieves but not remove the head of the queue, waiti
我是一名优秀的程序员,十分优秀!