非阻塞算法是否也适用于插入和删除或仅适用于某些条件,如无可用 space/ 无元素可消耗

Is non-blocking algorithm also apply for insertion and deletion OR only for some condition like no available space/ no elements to consume

一些博客提到如果没有space/element到add/consume,非阻塞算法数据结构不会阻塞调用线程。他们立即 return 异常或空。但是我在任何地方都找不到 insertion/deletion 在非阻塞算法中的工作原理。

当条件不满足时,可能会采取不同的操作。有四种典型形式:

  • 抛出异常
  • return特殊值
  • 超时

对于非阻塞实现,前两个可能会发生。

参考BlockingQueue的文档:

A Queue that additionally supports operations that wait for the queue to become non-empty when retrieving an element, and wait for space to become available in the queue when storing an element.

BlockingQueue methods come in four forms, with different ways of handling operations that cannot be satisfied immediately, but may be satisfied at some point in the future: one throws an exception, the second returns a special value (either null or false, depending on the operation), the third blocks the current thread indefinitely until the operation can succeed, and the fourth blocks for only a given maximum time limit before giving up. These methods are summarized in the following table:

         Throws exception   Special value   Blocks           Times out
Insert   add(e)             offer(e)        put(e)           offer(e, time, unit)
Remove   remove()           poll()          take()           poll(time, unit)
Examine  element()          peek()          not applicable   not applicable