当前位置:网站首页>Blocking Queue Analysis
Blocking Queue Analysis
2022-07-25 05:54:00 【Hide on jdk】

ArrayBlockQueue put Method : It's a lock , Judge whether the total number and length of the current array elements are the same , If the same block occurs , If it is less than the total number, it will be queued
public void put(E e) throws InterruptedException {
checkNotNull(e);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (count == items.length)
notFull.await();
enqueue(e);
} finally {
lock.unlock();
}
}
ArrayBlockQueue put And it's very simple : Determine whether the total is 0, If 0 Then block .
public E take() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (count == 0)
notEmpty.await();
return dequeue();
} finally {
lock.unlock();
}
}


List

//add by Hide on jdk If the team is full or empty, it will directly report an error
new Thread(()->{
try {
TimeUnit.SECONDS.sleep(1);
blockingQueue.add(1);
blockingQueue.add(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
new Thread(()->{
try {
System.out.println(blockingQueue.remove());
System.out.println("aaa");
TimeUnit.SECONDS.sleep(1);
System.out.println(blockingQueue.remove());
System.out.println("bbbb");
System.out.println(blockingQueue.remove());
System.out.println("ccc");
} catch (InterruptedException e) {
e.printStackTrace();
}
},"B").start();
//add by Hide on jdk Blocked when the team is full or empty
/*new Thread(()->{
try {
TimeUnit.SECONDS.sleep(1);
blockingQueue.put(1);
blockingQueue.put(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
new Thread(()->{
try {
System.out.println(blockingQueue.take());
System.out.println("aaa");
TimeUnit.SECONDS.sleep(1);
System.out.println(blockingQueue.take());
System.out.println("bbbb");
System.out.println(blockingQueue.take());
System.out.println("ccc");
} catch (InterruptedException e) {
e.printStackTrace();
}
},"B").start();*/
}
/**
* add The method is to add an element to the queue , If the queue is full , An exception will be thrown to indicate that the queue is full .
*/
private static void addTest() {
BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<Integer>(2);
System.out.println(blockingQueue.add(1));
System.out.println(blockingQueue.add(2));
System.out.println(blockingQueue.add(3));
}
/**
* remove The function of the method is to delete the element and return to the head node of the queue , If the deleted queue is empty , remove Method will throw an exception .
*/
private static void removeTest() {
ArrayBlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<Integer>(2);
blockingQueue.add(1);
blockingQueue.add(2);
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
}
/**
* element The method is to return the head node of the queue , But it doesn't delete . If the queue is empty , Throw an exception
*/
private static void elementTest() {
ArrayBlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<Integer>(2);
blockingQueue.element();
}
/**
* offer Method is used to insert an element . If the addition is successful, it will return true, And if the queue is full , return false
*/
private static void offerTest(){
ArrayBlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<Integer>(2);
System.out.println(blockingQueue.offer(1));
System.out.println(blockingQueue.offer(2));
System.out.println(blockingQueue.offer(3));
}
/**
* poll Method is also used to remove and return the head node of the queue . If the queue is empty , return null
*/
private static void pollTest() {
ArrayBlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<Integer>(3);
blockingQueue.offer(1);
blockingQueue.offer(2);
blockingQueue.offer(3);
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
}
/**
* peek Method returns the header element of the queue, but does not delete . If the queue is empty , return null
*/
private static void peekTest() {
ArrayBlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<Integer>(2);
System.out.println(blockingQueue.peek());
}
/**
* put The function of the method is to insert elements . If the queue is full, you cannot continue inserting , Blocking the insert thread , Until the queue is empty
*/
private static void putTest(){
BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<Integer>(2);
try {
blockingQueue.put(1);
blockingQueue.put(2);
blockingQueue.put(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* take Method is used to get and remove the head node of the queue . If there is no data in the execution queue , The block , Until there's data in the queue
*/
private static void takeTest(){
try {
blockingQueue.put(1);
blockingQueue.put(2);
System.out.println(blockingQueue.take());
System.out.println(blockingQueue.take());
System.out.println(blockingQueue.take());
System.out.println("bbb");
} catch (InterruptedException e) {
e.printStackTrace();
}
}LinkBlockingQueue:

offer Core source code : Less than the total capacity, directly enter the queue
public boolean offer(E e) {
if (e == null) throw new NullPointerException();
final AtomicInteger count = this.count;
if (count.get() == capacity)
return false;
int c = -1;
Node<E> node = new Node<E>(e);
final ReentrantLock putLock = this.putLock;
putLock.lock();
try {
if (count.get() < capacity) {
enqueue(node);
c = count.getAndIncrement();
if (c + 1 < capacity)
notFull.signal();
}
} finally {
putLock.unlock();
}
if (c == 0)
signalNotEmpty();
return c >= 0;
}
边栏推荐
- 10、渲染基础
- Xiaomi 12s UTRA Leica watermark generation online tool
- The u-collapse component of uniapp mobile uview is highly init
- A little experience about von Mises distribution
- y76.第四章 Prometheus大厂监控体系及实战 -- prometheus进阶(七)
- Leetcode/ integer division
- Switch NPM source to private source library
- PHP warehouse inventory management system source code WMS source code
- 剑指 Offer 05. 替换空格
- (15)[驱动开发]过写拷贝
猜你喜欢

(2022牛客多校二)K-Link with Bracket Sequence I(动态规划)

HTB-Optimum

Sword finger offer 45. arrange the array into the smallest number

Sword finger offer 05. replace spaces

QT qtextedit setting qscrollbar style sheet does not take effect solution

sqlilabs less-29

Leetcode 237. 删除链表中的节点
![(16) [system call] track system call (3 rings)](/img/b0/011351361135fd9f8e2d0d31749f73.png)
(16) [system call] track system call (3 rings)

Leetcode 202. happy number (not happy at all)

Leetcode 202. 快乐数(一点都不快乐)
随机推荐
Big talk · book sharing | Haas Internet of things device cloud integrated development framework
(15) [driver development] over written copy
编程大杂烩(一)
R language uses LM function to build multiple linear regression model and write regression equation according to model coefficient
(16)[系统调用]追踪系统调用(3环)
同条网线电脑正常上网,手机连接wifi成功,但是无法访问互联网
剑指 Offer 05. 替换空格
剑指 Offer 45. 把数组排成最小的数
剑指 Offer 32 - I. 从上到下打印二叉树
Microservice - hystrix fuse
HTB-Devel
The u-collapse component of uniapp mobile uview is highly init
Detailed explanation of stepn chain game system development mode (Sports money making mode)
2021 ICPC Shaanxi warm up match b.code (bit operation)
leetcode/整数除法
(2022 Niuke multi School II) l-link with level editor I (dynamic planning)
(2022牛客多校二)K-Link with Bracket Sequence I(动态规划)
ECS is exclusive to old users, and the new purchase of the remaining 10 instances is as low as 3.6% off
New discovery of ROS callback function
(14) [driver development] configuration environment vs2019 + wdk10 write XP driver