GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 更新 图解队列 · kings186/PassJava-Learning@b4d7c37

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • code/01.Threads/src/com/jackson0714/passjava/threads
    • QueueDemo.java
  • docs/10.并发多线程
    • 06.图解多线程.md
    • 06.图解队列.assets
      • image-20200907144652830.png
    • 06.图解队列.md
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎code/01.Threads/src/com/jackson0714/passjava/threads/QueueDemo.java‎

Copy file name to clipboardExpand all lines: code/01.Threads/src/com/jackson0714/passjava/threads/QueueDemo.java
+24-4Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
import com.sun.jmx.remote.internal.ArrayQueue;
44

55
import java.util.*;
6-
import java.util.concurrent.ArrayBlockingQueue;
7-
import java.util.concurrent.BlockingDeque;
8-
import java.util.concurrent.LinkedBlockingDeque;
9-
import java.util.concurrent.TransferQueue;
6+
import java.util.concurrent.*;
107

118
/**
129
* @Author: 悟空聊架构| PassJava666
@@ -26,6 +23,29 @@ public static void main(String[] args) {
2623
queue.add(100);
2724
queue.add(200);
2825

26+
ConcurrentLinkedQueue concurrentLinkedQueue = new ConcurrentLinkedQueue();
27+
28+
BuildingBlockWithName buildingBlock = new BuildingBlockWithName("三角形", "A");
29+
30+
concurrentLinkedQueue.add(buildingBlock);
31+
32+
LinkedList linkedList = new LinkedList();
33+
linkedList.add(buildingBlock);
34+
35+
List list = Collections.synchronizedList(new LinkedList<>());
36+
37+
ArrayDeque arrayDeque = new ArrayDeque();
38+
for (int i = 0; i < 50; i++) {
39+
arrayDeque.add(buildingBlock);
40+
}
41+
42+
BuildingBlockWithName buildingBlock1 = new BuildingBlockWithName("三角形", "A");
43+
BuildingBlockWithName buildingBlock2 = new BuildingBlockWithName("四边形", "B");
44+
ConcurrentLinkedDeque concurrentLinkedDeque = new ConcurrentLinkedDeque();
45+
concurrentLinkedDeque.addFirst(buildingBlock1);
46+
concurrentLinkedDeque.addLast(buildingBlock2);
47+
//结果:顺序:三角形、四边形
48+
2949
//TransferQueue;
3050
//int a = queue.peek();
3151
// PriorityQueue;
Collapse file

‎docs/10.并发多线程/06.图解多线程.md‎

Copy file name to clipboardExpand all lines: docs/10.并发多线程/06.图解多线程.md
  • Display the source diff
  • Display the rich diff
Whitespace-only changes.
Collapse file

‎docs/10.并发多线程/06.图解队列.assets/image-20200907144652830.png‎

Copy file name to clipboard
5.49 KB
  • Display the source diff
  • Display the rich diff
Loading
Collapse file

‎docs/10.并发多线程/06.图解队列.md‎

Copy file name to clipboardExpand all lines: docs/10.并发多线程/06.图解队列.md
+186-16Lines changed: 186 additions & 16 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public E element() {
213213

214214
## 六、阻塞缓冲BlockingQueue接口
215215

216-
### 3.1 宏观来看BlockingQueue(阻塞队列)
216+
### 6.1 宏观来看BlockingQueue(阻塞队列)
217217

218218
![阻塞队列](http://cdn.jayh.club/blog/20200906/K8rc4tWINGSl.png?imageslim)
219219

@@ -229,7 +229,7 @@ public E element() {
229229

230230
(6)为什么要用阻塞队列:生产者生产和消费者消费的速率不一样,需要用队列来解决速率差问题,当队列满了或空的时候,则需要阻塞生产或消费动作来解决队列满或空的问题。
231231

232-
### 3.2 案例解析
232+
### 6.2 案例解析
233233

234234
线程A往阻塞队列(Blocking Queue)中`添加`元素,而线程B从阻塞队列中`移除`元素。
235235

@@ -240,7 +240,7 @@ public E element() {
240240
- 生活中的案例:去海底捞吃火锅的时候,人太多了,需要排号,等其他桌空出来了才能进去。
241241
- 翻译成线程:线程A往阻塞队列中添加元素,将队列填满了,线程B现在正在忙,无法拿出队列中的元素,所以阻塞队列没有地方再放元素了,这个时候线程A添加元素的操作就被阻塞了
242242

243-
### 3.3 操刀BlockingQueue接口
243+
### 6.3 操刀BlockingQueue接口
244244

245245
**BlockingQueue接口的10个核心方法:**
246246

@@ -271,12 +271,12 @@ public E element() {
271271
- element方法用于检测头部元素的存在性,如果队列为空,则抛出异常,否则返回头部元素。
272272
- peek方法用于检测头部元素的存在性,如果队列为空,返回特殊值null,否则返回头部的元素。
273273

274-
### 3.4 哪些类继承了BlockingQueue接口?
274+
### 6.4 哪些类继承了BlockingQueue接口?
275275

276276
- BlockingDeque接口 - 双端阻塞队列
277-
- TransferQueue接口 -
277+
- TransferQueue接口 - 传输队列
278278

279-
### 3.5 哪些类实现了BlockingQueue接口?
279+
### 6.5 哪些类实现了BlockingQueue接口?
280280

281281
- ArrayBlockingQueue类 - 由数组构成的有界阻塞队列
282282
- LinkedBlockingQueue类 - 由链表构成的有界阻塞队列,界限默认大小为Integer.MAX_Value(2^31-1),值非常大,相当于无界。
@@ -286,6 +286,10 @@ public E element() {
286286
- LinkedTransferQueue类 - 由链表构成的无界阻塞TransferQueue队列
287287
- DelayQueue类 - 使用优先级队列实现的延迟无界阻塞队列
288288

289+
### 6.6 BlockingQueue接口继承了哪些接口
290+
291+
- Queue接口,可作为队列使用
292+
289293
## 七、双端阻塞BlockingDeque接口
290294

291295
### 7.1 BlockingDeque接口方法
@@ -323,8 +327,10 @@ queue.addLast("400");
323327

324328
### 7.4 BlockingDeque接口继承了哪些接口?
325329

326-
- Deque接口
327-
- BlockingQueue接口
330+
- Queue接口,具有队列的功能
331+
332+
- Deque接口,具有双端队列的功能
333+
- BlockingQueue接口,可作为阻塞队列使用
328334

329335
### 7.5 哪些类实现了BlockDeque接口?
330336

@@ -372,23 +378,187 @@ queue.addLast("400");
372378
- 获取是否有通过BlockingQueue.tabke()方法或超时限制poll方法等待接受元素的消费者。
373379
- 返回true则表示至少有一个等待消费者。
374380

381+
### 8.3 TransferQueue接口继承了哪些接口?
382+
383+
- BlockingQueue接口,可作为阻塞队列使用
384+
- Queue接口,可作为队列使用
385+
386+
### 8.4 哪些类实现了TransferQueue接口?
387+
388+
- LinkedTranferQueue接口
389+
375390
## 九、优先由你PriorityQueue类
376391

377-
## 十、ConcurrentLinkedQueue类
392+
### 9.1 理解PriorityQueue类
393+
394+
- PriorityQueue是一个支持优先级的无界阻塞队列。
395+
396+
- 默认自然顺序升序排序。
397+
- 可以通过构造参数Comparator来对元素进行排序。
398+
399+
``` java
400+
public PriorityQueue(Comparator<? super E> comparator) {
401+
this(DEFAULT_INITIAL_CAPACITY, comparator);
402+
}
403+
```
404+
405+
- 自定义实现comapreTo()方法来指定元素排序规则。
406+
407+
```java
408+
public Comparator<? super E> comparator() {
409+
return comparator;
410+
}
411+
```
412+
413+
- 不允许插入null元素。
414+
- 实现PriorityQueue接口的类,不保证线程安全,除非是PriorityBlockingQueue。
415+
- PriorityQueue的迭代器不能保证以任何特定顺序遍历元素,如果需要有序遍历,请考虑使用`Arrays.sort(pq.toArray)`。
416+
- 进列(`offer`、`add`)和出列( `poll`、`remove()`)的时间复杂度O(log(n))。
417+
- remove(Object) 和 contains(Object)的算法时间复杂度O(n)。
418+
- peek、element、size的算法时间复杂度为O(1)。
419+
420+
### 9.2 PriorityQueue类继承了哪些类?
421+
422+
- AbstractQueue抽象类,具有队列的功能
423+
424+
### 9.2 PriorityQueue类实现了哪些接口?
425+
426+
- Queue接口,可作为队列使用。
427+
428+
## 十、双向链表LinkedList类
429+
430+
### 10.1 LinkedList的结构
431+
432+
- LinkedList实现了List和Deque接口,所以是一种双链表结构,可以当作堆栈、队列、双向队列使用。
433+
- 一个双向列表的每一个元素都有三个整数值:元素、向后的节点链接、向前的节点链接
434+
435+
![mark](http://cdn.jayh.club/blog/20200907/144716761.png)
436+
437+
我们来看下节点类Node
438+
439+
```java
440+
private static class Node<E> {
441+
E item; //元素
442+
Node<E> next; //向后的节点链接
443+
Node<E> prev; //向前的节点链接
444+
445+
Node(Node<E> prev, E element, Node<E> next) {
446+
this.item = element;
447+
this.next = next;
448+
this.prev = prev;
449+
}
450+
}
451+
```
452+
453+
### 10.2 与ArrayList的区别
454+
455+
- 1.LinkedList的增加和删除效率相对较高,而查找和修改的效率相对较低。
456+
457+
- 2.以下情况建议使用ArrayList
458+
- 频繁访问列表中的一个元素。
459+
- 只在列表的首尾添加元素。
460+
- 3.以下情况建议使用LinkedList
461+
- 频繁地在列表开头、中间、末尾添加和删除元素。
462+
- 需要通过循环迭代来访问列表中的元素。
463+
464+
### 10.3 LinkedList不是线程安全的
465+
466+
LinkedList不是线程安全的,所以可以使用如下方式保证线程安全。
467+
468+
```java
469+
List list = Collections.synchronizedList(new LinkedList<>());
470+
```
471+
472+
### 10.4 LinkedList的家庭成员关系
473+
474+
- LinkedList 继承了 AbstractSequentialList 类。
475+
476+
- LinkedList 实现了 Queue 接口,可作为队列使用。
477+
- LinkedList 继承了 AbstractQueue抽象类,具有队列的功能。
478+
- LinkedList 实现了 List 接口,可进行列表的相关操作。
479+
- LinkedList 实现了 Deque 接口,可作为双向队列使用。
480+
- LinkedList 实现了 Cloneable 接口,可实现克隆。
481+
- LinkedList 实现了 java.io.Serializable 接口,即可支持序列化,能通过序列化去传输。
482+
483+
## 十一、并发安全ConcurrentLinkedQueue类
484+
485+
### 10.1 理解ConcurrentLinkedQueue
486+
487+
- ConcurrentLinked是由链表结构组成的线程安全的先进先出无界队列。
488+
- 当多线程要共享访问集合时,ConcurrentLinkedQueue是一个比较好的选择。
489+
- 不允许插入null元素
490+
- 支持非阻塞地访问并发安全的队列,不会抛出ConcurrentModifiationException异常。
491+
- size方法不是准确的,因为在统计集合的时候,队列可能正在添加元素,导致统计不准。
492+
- 批量操作addAll、removeAll、retainAll、containsAll、equals和toArray不保证原子性(操作不可分割)
493+
- 添加元素happen-before其他线程移除元素。
494+
- 用法如下:
495+
496+
```java
497+
ConcurrentLinkedQueue queue = new ConcurrentLinkedQueue();
498+
BuildingBlockWithName buildingBlock = new BuildingBlockWithName("三角形", "A");
499+
concurrentLinkedQueue.add(buildingBlock);
500+
```
501+
502+
### 10.2 ConcurrentLinkedQueue类继承了哪些类?
503+
504+
- AbstractQueue抽象类,具有队列的功能
505+
506+
### 10.3 ConcurrentLinkedQueue类实现了哪些接口?
507+
508+
- Queue接口,可作为队列使用
509+
510+
## 十二、双向数组ArrayDeque类
511+
512+
### 12.1 理解ArrayDeque
513+
514+
- 由数组组成的双端队列。
515+
- 没有容量限制,根据需要扩容。
516+
- 不是线程安全的。
517+
- 禁止插入null元素。
518+
- 当用作栈时,比栈速度快,当用作队列时,速度比LinkList快。
519+
- 大部分方法的算法时间复杂度为O(1)。
520+
- remove、removeFirstOccurrence、removeLastOccurrence、contains、remove 和批量操作的算法时间复杂度O(n)
521+
522+
### 12.2 使用方法
523+
524+
创建一个ArrayDeque,往arrayDeque队尾添加元素。
525+
526+
```java
527+
ArrayDeque arrayDeque = new ArrayDeque();
528+
for (int i = 0; i < 50; i++) {
529+
arrayDeque.add(buildingBlock); // add方法等价于addLast方法
530+
}
531+
```
532+
533+
### 12.3 ArrayDeque实现了哪些接口
534+
535+
- Deque接口 - 可用于双端队列
536+
537+
## 十三、双向并发ConcurrentLinkedDeque类
538+
539+
### 13.1 理解ConcurrentLinkedDeque类
540+
541+
- 由链表结构组成的双向无界阻塞队列
542+
- 插入、删除和访问操作可以并发进行,线程安全的类
543+
- 不允许插入null元素
544+
- 在并发场景下,计算队列的大小是不准确的,因为计算时,可能有元素加入队列。
545+
- 批量操作addAll、removeAll、retainAll、containsAll、equals和toArray不保证原子性(操作不可分割)
546+
547+
378548

379-
## 十一、LinkedList类
549+
## 十四、链表阻塞LinkedBlockingQueue类
380550

381-
## 十二、ArrayDeque类
551+
## 十五、双向阻塞LinkedBlockingDeque类
382552

383-
## 十三、ConcurrentLinkedDeque类
553+
## 十六、传输阻塞LinkedTransferQueue类
384554

385-
## 十四、ArrayBlockingQueue类
555+
## 十七、数组阻塞ArrayBlockingQueue类
386556

387-
## 十五、传球手SynchronousQueue类
557+
## 十八、传球好手SynchronousQueue类
388558

389-
## 十六、PriorityBlockQueue类
559+
## 十九、优先级阻塞PriorityBlockQueue类
390560

391-
## 十七、DelayQueue类
561+
## 二十、延时阻塞DelayQueue类
392562

393563

394564

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.