Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the LinkedBlockingQueue of JUC

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to realize the LinkedBlockingQueue of JUC". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

From the word Blocking, we can infer that LinkedBlockingQueue is a blocking queue. We have previously introduced the difference in implementation between blocking queue and non-blocking queue. We know that blocking queue is generally based on lock mechanism to ensure thread safety. In this paper, let's analyze how LinkedBlockingQueue builds thread safety queue based on lock.

Also from the Linked keyword, we can infer that the underlying LinkedBlockingQueue depends on the linked list implementation, and a single linked list is implemented inside the LinkedBlockingQueue to store queue elements. The node Node class is defined as follows:

Static class Node {E item; Node next; Node (E x) {item = x;}}

The pointing of the next pointer can be divided into three cases:

Point to a specific successor node.

Pointing to yourself means that the successor node is head.next.

Point to null, indicating that the current node is the tail node of the queue and there is no successor node.

LinkedBlockingQueue defines the head and last pointers to the head and tail nodes of the queue, respectively. In addition, LinkedBlockingQueue defines the following fields:

Public class LinkedBlockingQueue extends AbstractQueue implements BlockingQueue, Serializable {/ * * current queue capacity limit * / private final int capacity; / * * record the number of elements in the current queue * / private final AtomicInteger count = new AtomicInteger (); / * * queue head node * / transient Node head; / * * queue tail node * / private transient Node last / * * used to control take, poll and other operations to ensure that only one thread gets the element from the queue at a time * / private final ReentrantLock takeLock = new ReentrantLock (); / * * conditional queue, recording the thread waiting because the queue is empty * / private final Condition notEmpty = takeLock.newCondition () / * * users control put, offer and other operations to ensure that only one thread adds elements to the queue at the same time * / private final ReentrantLock putLock = new ReentrantLock (); / * * conditional queue, the thread waiting because the queue is full * / private final Condition notFull = putLock.newCondition (); / /. Omit method definition}

As you can see from the above field definition, LinkedBlockingQueue limits the capacity limit of the queue and uses the AtomicInteger type field to count the number of elements in the queue. Although the underlying LinkedBlockingQueue relies on a linked list implementation and is theoretically unbounded, LinkedBlockingQueue implements an upper limit on the capacity of the queue (default is Integer.MAX_VALUE).

In addition, for the out-queue and in-queue operations, LinkedBlockingQueue sets up an exclusive reentrant lock, namely takeLock and putLock, respectively, so as to ensure that only one thread performs the out-queue operation and only one thread performs the de-queue operation at the same time, and the dequeued thread and the queued thread do not influence each other. For some blocking versions of the dequeuing method, if the queue is empty, the dequeued thread will be recorded in the conditional queue notEmpty to wait, and if the queue is full, the incoming thread will be recorded in the conditional queue notFull to wait.

BlockingQueue interface

The BlockingQueue interface inherits from the Queue interface and is used to describe blocking queues. When a queue fails to respond to a user's request in a timely manner, for example, when we try to get elements from an empty queue, or continue to add elements to a bounded queue that is full, BlockingQueue defines the following four response forms:

Throw an exception.

Returns a special value immediately, such as null or false.

Blocks the current request indefinitely until the queue status becomes available.

Timeout blocks the current request until the queue status becomes available.

The definition of BlockingQueue interface is as follows:

Public interface BlockingQueue extends Queue {boolean offer (E e); boolean offer (E, long timeout, TimeUnit unit) throws InterruptedException; boolean add (E e); void put (E) throws InterruptedException; E poll (long timeout, TimeUnit unit) throws InterruptedException; E take () throws InterruptedException; boolean remove (Object o); boolean contains (Object o); int remainingCapacity (); int drainTo (Collection)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report