首页 > 试题广场 >

下面几个关于Java里queue的说法哪些是正确的()?

[不定项选择题]
下面几个关于Javaqueue的说法哪些是正确的()?
  • LinkedBlockingQueue是一个可选有界队列,不允许null值
  • PriorityQueue,LinkedBlockingQueue都是线程不安全的
  • PriorityQueue是一个无界队列,不允许null值,入队和出队的时间复杂度是O(log(n))
  • PriorityQueue,ConcurrentLinkedQueue都遵循FIFO原则

正确答案
AC
答案解析
A、LinkedBlockingQueue是一个基于节点链接的可选是否有界的阻塞队列,不允许null值。
B、LinkedBlockingQueue是一个线程安全的阻塞队列,实现了先进先出等特性。
C、PriorityQueue是一个***队列,不允许null值,入队和出队的时间复杂度是O(log(n))。
D、PriorityQueue是不同于先进先出队列的另一种队列。每次从队列中取出的是具有最高优先权的元素。ConcurrentLinkedQueue是一个基于链接节点的***线程安全队列,该队列的元素遵循FIFO原则。

发表于 2018-04-16 17:14:57 回复(15)
成功避开了正确答案
发表于 2017-09-18 09:05:48 回复(45)
以下翻译来自java8的官方文档:

1、LinkedBlockingQueue:基于链接节点的可选限定的blocking queue 。 这个队列排列元素FIFO(先进先出)。 队列的头部是队列中最长的元素。 队列的尾部是队列中最短时间的元素。 新元素插入队列的尾部,队列检索操作获取队列头部的元素。 链接队列通常具有比基于阵列的队列更高的吞吐量,但在大多数并发应用程序中的可预测性能较低。
blocking queue说明:不接受null元素;可能是容量有限的;实现被设计为主要用于生产者 - 消费者队列;不支持任何类型的“关闭”或“关闭”操作,表示不再添加项目实现是线程安全的;


2、PriorityQueue:
2.1、基于优先级堆的无限优先级queue 。 优先级队列的元素根据它们的有序natural ordering ,或由一个Comparator在队列构造的时候提供,这取决于所使用的构造方法。 优先队列不允许null元素。 依靠自然排序的优先级队列也不允许插入不可比较的对象(这样做可能导致ClassCastException )。
2.2、该队列的头部是相对于指定顺序的最小元素。 如果多个元素被绑定到最小值,那么头就是这些元素之一 - 关系被任意破坏。 队列检索操作poll , remove , peek和element访问在队列的头部的元件。
2.3、优先级队列是无限制的,但是具有管理用于在队列上存储元素的数组的大小的内部容量 。 它始终至少与队列大小一样大。 当元素被添加到优先级队列中时,其容量会自动增长。 没有规定增长政策的细节。
2.4、该类及其迭代器实现Collection和Iterator接口的所有可选方法。 方法iterator()中提供的迭代器不能保证以任何特定顺序遍历优先级队列的元素。 如果需要有序遍历,请考虑使用Arrays.sort(pq.toArray()) 。
2.5、请注意,此实现不同步。 如果任何线程修改队列,多线程不应同时访问PriorityQueue实例。 而是使用线程安全的PriorityBlockingQueue类。
实现注意事项:此实现提供了O(log(n))的时间入队和出队方法( offer , poll , remove()和add ); remove(Object)和contains(Object)方法的线性时间; 和恒定时间检索方法( peek , element和size )。


3、ConcurrentLinkedQueue:基于链接节点的***并发deque(deque是双端队列) 。 并发插入,删除和访问操作可以跨多个线程安全执行。 A ConcurrentLinkedDeque是许多线程将共享对公共集合的访问的适当选择。像大多数其他并发集合实现一样,此类不允许使用null元素。
编辑于 2018-01-16 19:39:56 回复(9)

正确答案: A C   你的答案: B D (错误)

发表于 2018-07-24 10:56:31 回复(4)

以下内容摘自JDK7官方文档

PriorityQueue

An unbounded(***) priority queue based on a priority heap. The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time(基于比较器排序), depending on which constructor is used. A priority queue does not permit null elements(不允许空值). A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in ClassCastException).
The head of this queue is the least element with respect to the specified ordering. If multiple elements are tied for least value, the head is one of those elements -- ties are broken arbitrarily. The queue retrieval operations poll, remove, peek, and element access the element at the head of the queue.

A priority queue is unbounded, but has an internal capacity governing the size of an array used to store the elements on the queue. It is always at least as large as the queue size. As elements are added to a priority queue, its capacity grows automatically. The details of the growth policy are not specified.

This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces. The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

Note that this implementation is not synchronized(线程不安全). Multiple threads should not access a PriorityQueue instance concurrently if any of the threads modifies the queue. Instead, use the thread-safe PriorityBlockingQueue class.

Implementation note: this implementation provides O(log(n))(时间复杂度) time for the enqueing and dequeing methods (offer, poll, remove() and add); linear time for the remove(Object) and contains(Object) methods; and constant time for the retrieval methods (peek, element, and size).

LinkedBlockingQueue

An optionally-bounded(可选有界) blocking queue based on linked nodes. This queue orders elements FIFO (first-in-first-out). The head of the queue is that element that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time. New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue. Linked queues typically have higher throughput than array-based queues but less predictable performance in most concurrent applications.
The optional capacity bound constructor argument serves as a way to prevent excessive queue expansion. The capacity, if unspecified, is equal to Integer.MAX_VALUE. Linked nodes are dynamically created upon each insertion unless this would bring the queue above capacity.

This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces.(如果没有说时线程不安全,那么默认就是线程安全)

发表于 2017-08-20 09:54:51 回复(5)

常见的阻塞队列

  • ArrayBlockingQueue:基于数组,在创建ArrayBlockingQueue对象时必须制定容量大小,先进先出队列,有界队列容量有上限
  • LinkedBlockingQueue:基于链表,在创建LinkedBlockingQueue对象时如果不指定容量大小,默认大小为Integer.MAX_VALUE,先进先出队列,有界队列容量有上限
  • PriorityBlockingQueue:按照元素的优先级对元素进行排序,按照优先级顺序出队,每次出队的元素都是优先级最高的元素。注意,此阻塞队列为无界阻塞队列,即容量没有上限。

发表于 2020-07-29 17:11:21 回复(0)
LinkeBlockingQueue:
1.基于结点链接的阻塞线程队列,不允许null值
2.线程安全,既然是阻塞线程的队列,自然FIFO
PriorityQueue:
1.***队列,不允许Null值,时间复杂度为O(log(n))
2.与常规的FIFO栈不同,这个栈每次只返回优先级最高的元素
ConcurrentLinkedQueue:
1.一个基于结点的***的队列,遵循线程安全和FIFO

发表于 2018-09-10 11:25:24 回复(1)
队列还是用的太少了。。。
发表于 2017-10-12 15:52:30 回复(0)
LinkedBlockingQueue是线程安全的,所以B不对;
PriorityQueue底层实现是小根堆,不是FIFO的,所以D不对

发表于 2017-08-24 10:17:57 回复(1)
这个太难记了
发表于 2019-03-04 17:22:18 回复(0)
LinkedBlockingQueue是一个线程安全的阻塞队列,PriorityQueue是非线程安全的
PriorityQueue是不同于先进先出队列的另一种队列。每次从队列中取出的是具有最高优先权的元素。ConcurrentLinkedQueue是一个基于链接节点的无界线程安全队列,该队列的元素遵循FIFO原则。


发表于 2022-09-27 20:42:23 回复(0)
感觉平常不怎么用这些类的说
编辑于 2020-05-16 23:25:58 回复(0)

尴尬😅

发表于 2018-08-08 15:58:38 回复(0)
PriorityQueue底层实现是小根堆,不是FIFO的
发表于 2018-04-07 12:14:52 回复(0)
A:可选:即可选是否有界(他有多个构造器,来初始化capacity,其中capacity是final的);不允许空值 在他的put和offer等方法中有体现
--------------------------------------------------------------------------------------------------------------------------------------------
B。PriorityQueue是非线程安全的(具体可以查看他的方法)LinkedBlockingQueue是线程安全的
C. 优先级队列实现原理是最大堆,(可以百度一下最大堆)
D. 优先级队列是优先级高的先出队列。
发表于 2022-01-28 13:39:54 回复(0)
LinkedBlockingQueue是一个线程安全的阻塞队列,实现了先进先出等特性。

发表于 2021-10-30 16:26:19 回复(0)
A,C
发表于 2019-05-07 09:20:37 回复(0)
两个拿来比较出题得   有“都”字的一般都错
发表于 2018-06-25 09:01:21 回复(0)
优先级队列是基于大或者小顶堆实现的,这个可以选择配置,所以出入队的时间复杂度是LOG(N),不遵循FIFO规则,优先级队列中的值必须是可比较的,所以不允许是null

发表于 2024-01-19 20:27:09 回复(0)
C选项,优先级队列PriorityQueue出队的时间复杂度是O1,比如poll,remove,但是需要重新维护堆结构,所以时间复杂度是On
发表于 2023-08-23 09:11:42 回复(0)