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 — 更新 图解队列 · owen2016/PassJava-Learning@30068d3

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.图解队列.assets
      • image-20200906233348321.png
    • 06.图解队列.md
    • Balsamiq
      • 队列.bmpr
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
+12-4Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import java.util.*;
66
import java.util.concurrent.ArrayBlockingQueue;
7+
import java.util.concurrent.BlockingDeque;
8+
import java.util.concurrent.LinkedBlockingDeque;
79
import java.util.concurrent.TransferQueue;
810

911
/**
@@ -15,12 +17,18 @@
1517
public class QueueDemo {
1618

1719
public static void main(String[] args) {
18-
ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<>(2);
20+
//ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<>(2);
21+
LinkedBlockingDeque queue = new LinkedBlockingDeque();
22+
queue.addFirst("test1");
23+
queue.addFirst(300);
24+
queue.addLast("400");
25+
1926
queue.add(100);
2027
queue.add(200);
21-
int a = queue.peek();
22-
PriorityQueue;
23-
Deque
28+
//int a = queue.peek();
29+
// PriorityQueue;
30+
// Deque;
31+
// LinkedBlockingDeque;
2432

2533
}
2634
}
Collapse file

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

Copy file name to clipboard
132 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
+121-35Lines changed: 121 additions & 35 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ ArrayBlockingQueue 默认不保证线程访问的公平性
3333

3434
## 一、队列自我介绍
3535

36-
![队列原理图](http://cdn.jayh.club/blog/20200906/CImqfwMGuSkt.png?imageslim)
36+
![队列原理图](http://cdn.jayh.club/blog/20200906/5EuhKrlR0TDp.png?imageslim)
3737

38-
我叫队列,无论现实生活中还是计算机的世界中,我都是一个很重要的角色哦~
38+
hi,大家好,我叫队列,无论现实生活中还是计算机的世界中,我都是一个很重要的角色哦~
3939

4040
我是一种数据结构,大家可以把我想象成一个数组,元素从我的一头进入、从另外一头出去,称为FIFO原则(先进先出原则)。
4141

@@ -57,12 +57,13 @@ ArrayBlockingQueue 默认不保证线程访问的公平性
5757

5858
弄清楚下面的继承实现关系对后面理解18种队列有很大帮助。
5959

60-
![18个Queue的继承实现关系图](http://cdn.jayh.club/blog/20200906/EAHahXHxQAyM.png?imageslim)
60+
![18个Queue的继承实现关系图](http://cdn.jayh.club/blog/20200906/LrXN2pKsedQT.png?imageslim)
6161

6262
- `Queue`接口**继承**`Collection`接口,`Collection`接口**继承** `Iterable`接口
6363
- `BlockingQueue`接口、`Deque`接口 **继承** `Queue`接口
6464
- `AbstractQueue`抽象类**实现**`Queue`接口
6565
- `BlockingDeque`接口、`TransferQueue`接口**继承**`BlockingQueue`接口
66+
- `BlockingDeque`接口继承`Deque`接口
6667
- `LinkedBlockingDeque`类**实现**`BlockingDeque`接口
6768
- `LinkedTransferQueue`类接口**实现**`TransferQueue`接口
6869
- `LinkedList`类、`ArrayDeque`类、`ConcurrentLinkedDeque`类**实现** 了`Deque`接口
@@ -76,15 +77,15 @@ ArrayBlockingQueue 默认不保证线程访问的公平性
7677
- 接口继承接口,用 extends
7778
- 类继承类,用extends
7879

79-
## 三、庖丁解牛Queue接口
80+
## 三、万物归宗Queue接口
8081

8182
### 2.1 深入理解Queue接口的本质
8283

8384
- Queue接口是一种Collection,被设计用于处理之前临时保存在某处的元素。
8485

8586
- 除了基本的Collection操作之外,队列还提供了额外的插入、提取和检查操作。每一种操作都有两种形式:如果操作失败,则抛出一个异常;如果操作失败,则返回一个特殊值(null或false,取决于是什么操作)。
8687

87-
- 队列通常是以FIFO(先进先出)的方式排序元素,但是不是必须的。
88+
- 队列通常是以FIFO(先进先出)的方式排序元素,但是这不是必须的。
8889

8990
- 只有优先级队列可以根据提供的比较器对元素进行排序或者是采用正常的排序。无论怎么排序,队列的头将通过调用remove()或poll()方法进行移除。在FIFO队列种,所有新的元素被插入到队尾。其他种类的队列可能使用不同的布局来存放元素。
9091
- 每个Queue必须指定排序属性。
@@ -112,7 +113,9 @@ ArrayBlockingQueue 默认不保证线程访问的公平性
112113

113114
## 四、双端队列Deque接口
114115

115-
![双端队列Deque](http://cdn.jayh.club/blog/20200906/jUTR0N36tMhV.png?imageslim)
116+
### 4.1 深入理解Deque接口的原理
117+
118+
![双端队列Deque](http://cdn.jayh.club/blog/20200906/rDCDv92bQePj.png?imageslim)
116119

117120
**(1)Deque概念:**支持两端元素插入和移除的线性集合。名称`deque`是双端队列的缩写,通常发音为`deck`。大多数实现Deque的类,对它们包含的元素的数量没有固定的限制的,支持有界和无界。
118121

@@ -143,7 +146,20 @@ ArrayBlockingQueue 默认不保证线程访问的公平性
143146

144147
![mark](http://cdn.jayh.club/blog/20200906/8w6fOGdR0uVo.png?imageslim)
145148

146-
## 五、队列骨架 AbstractQueue抽象类
149+
### 4.1 哪些类实现了Deque接口
150+
151+
- LinkedList类
152+
- ArrayDeque类
153+
- ConcurrentLinkedDeque类
154+
- LinkedBlockingDeque类
155+
156+
### 4.2 哪些类继承了Deque接口
157+
158+
- BlockingDeque接口
159+
160+
## 五、队列骨架 AbstractQueue
161+
162+
### 5.1 深入理解AbstractQueue抽象类
147163

148164
AbstractQueue是一个抽象类,继承了Queue接口,提供了一些Queue操作的骨架实现。
149165

@@ -186,11 +202,16 @@ public E element() {
186202
}
187203
```
188204

189-
如果继承抽象类则必须保证offer方法不允许null值插入。
205+
注意:
190206

207+
- 如果继承抽象类则必须保证offer方法不允许null值插入。
191208

209+
### 5.2 哪些类继承了AbstractQueue抽象类
192210

193-
## 四、BlockingQueue
211+
- `ArrayBlockingQueue`类、`LinkendBlockingQueue`类、`LinkedBlockingDeque`类、`LinkedTransferQueue`类、`SynchronousQueue`类、`PriorityBlockQueue`类、`DelayQueue类`**继承** 了`AbstractQueue`抽象类
212+
- `PriorityQueue`类和`ConcurrentLinkedQueue`类**继承** 了`AbstractQueue`抽象类
213+
214+
## 六、阻塞队列BlockingQueue
194215

195216
### 3.1 宏观来看BlockingQueue(阻塞队列)
196217

@@ -208,16 +229,6 @@ public E element() {
208229

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

211-
(7)Java中的阻塞队列有7个,都继承BlockingQueue接口:
212-
213-
- ArrayBlockingQueue 由数组构成的有界阻塞队列
214-
- LinkedBlockingQueue 由链表构成的有界阻塞队列,界限默认大小为Integer.MAX_Value(21亿),值非常大,相当于无界。
215-
- LinkedBlockingDeque 由链表构成的双向阻塞队列
216-
- LinkedTransferQueue 由链表构成的无界阻塞队列
217-
- SynchronousQueue 不存储元素的阻塞队列,只做数据传递
218-
- LinkedTransferQueue 由链表构成的无界阻塞TransferQueue队列
219-
- DelayQueue 使用优先级队列实现的延迟无界阻塞队列
220-
221232
### 3.2 案例解析
222233

223234
线程A往阻塞队列(Blocking Queue)中`添加`元素,而线程B从阻塞队列中`移除`元素。
@@ -231,38 +242,113 @@ public E element() {
231242

232243
### 3.3 操刀BlockingQueue接口
233244

234-
**BlockingQueue接口的12个核心方法:**
245+
**BlockingQueue接口的10个核心方法:**
246+
247+
![继承的方法](http://cdn.jayh.club/blog/20200906/7dHpxPTefTnI.png?imageslim)
235248

236-
![BlockingQueue接口的12个核心方法](http://cdn.jayh.club/blog/20200906/jz9DxBDCqWya.png?imageslim)
249+
**10个核心方法总结如下:**
237250

238-
有三大类操作:插入、移除、检查
251+
![BlockingQueue接口的10个核心方法](http://cdn.jayh.club/blog/20200906/cjFCGpvV9uE6.png?imageslim)
239252

240-
- 插入有四种方法:add、offer、put、offer超时版。
253+
有三大类操作:插入、移除、检查。
254+
255+
- **插入有四种方法:** add、offer、put、offer超时版。
241256
- add方法特别之处用于添加失败时抛出异常,共有四种异常:
242-
- IllegalStateException - 队列满了
243-
- ClassCastException - 添加的元素类型不匹配
244-
- NullPointerException - 添加的元素为null
245-
- IllegalArgumentException - 添加的元素某些属性不匹配
257+
- `IllegalStateException` - 队列满了
258+
- `ClassCastException` - 添加的元素类型不匹配
259+
- `NullPointerException` - 添加的元素为null
260+
- `IllegalArgumentException` - 添加的元素某些属性不匹配
246261
- offer方法特别之处用于添加失败时只返回false
247262
- put方法特别之处用于当阻塞队列满时,生产者如果往队列里put元素,则队列会一直阻塞生产者线程,直到队列可用或者响应中断退出
248263
- offer超时方法特别之处用于当阻塞队列满时,生产者如果往队列里面插入元素,队列会阻塞生产者线程一段时间,如果超过了指定时间,生产者线程会退出,并返回false。
249-
- 移除有四种方法:remove、poll、take、poll超时版
264+
- **移除有四种方法:** remove、poll、take、poll超时版
250265
- remove方法特别之处用于移除失败时抛出异常
251-
- NoSuchElementException - 如果这个队列是空的
266+
- `NoSuchElementException` - 如果这个队列是空的
252267
- poll方法特别之处用于移除失败时返回null
253268
- take方法特别之处用于当阻塞队列为空时,消费者线程如果从队列里面移除元素,则队列会一直阻塞消费者线程,直到队列不为空
254269
- poll超时方法特别之处用于当阻塞队列空时,消费者如果从队列里面删除元素,则队列会一直阻塞消费者线程,如果超过了指定时间,消费者线程会退出,并返回null
255-
- 检查有两种方法:element、peek
270+
- **检查有两种方法:** element、peek
256271
- element方法用于检测头部元素的存在性,如果队列为空,则抛出异常,否则返回头部元素。
257-
- peek方法用于检测头部元素的存在性,如果队列为空,返回特殊值null,否则返回头部的元素
272+
- peek方法用于检测头部元素的存在性,如果队列为空,返回特殊值null,否则返回头部的元素。
273+
274+
### 3.4 哪些类继承了BlockingQueue接口?
275+
276+
- BlockingDeque接口 - 双端阻塞队列
277+
- TransferQueue接口 -
278+
279+
### 3.5 哪些类实现了BlockingQueue接口?
280+
281+
- ArrayBlockingQueue类 - 由数组构成的有界阻塞队列
282+
- LinkedBlockingQueue类 - 由链表构成的有界阻塞队列,界限默认大小为Integer.MAX_Value(21亿),值非常大,相当于无界。
283+
- LinkedBlockingDeque类 - 由链表构成的双向阻塞队列
284+
- LinkedTransferQueue类 - 由链表构成的无界阻塞队列
285+
- SynchronousQueue类 - 不存储元素的阻塞队列,只有一个元素进行数据传递。
286+
- LinkedTransferQueue类 - 由链表构成的无界阻塞TransferQueue队列
287+
- DelayQueue类 - 使用优先级队列实现的延迟无界阻塞队列
288+
289+
## 七、BlockingDeque接口
290+
291+
### 7.1 BlockingDeque接口方法
292+
293+
是阻塞和双向队列接口的结合。有如下方法:
294+
295+
![mark](http://cdn.jayh.club/blog/20200907/Ar0RX1xQKRB7.png?imageslim)
296+
297+
示例:
298+
299+
尝试执行以下方法:
300+
301+
``` java
302+
LinkedBlockingDeque queue = new LinkedBlockingDeque();
303+
queue.addFirst("test1");
304+
queue.addFirst(300);
305+
queue.addLast("400");
306+
```
307+
308+
最后队列中的元素顺序如下:
309+
310+
300, test1, 400
311+
312+
![mark](http://cdn.jayh.club/blog/20200907/xyU7h59rcTVc.png?imageslim)
313+
314+
### 7.2 BlockDeque和BlockQueue的对等方法
315+
316+
![mark](http://cdn.jayh.club/blog/20200907/ng7WKFnSDU7S.png?imageslim)
317+
318+
### 7.3 BlockingDeque的特点
319+
320+
- 线程安全。
321+
- 不允许使用null元素。
322+
- 无界和有界都可以。
323+
324+
### 7.4 BlockingDeque接口继承了哪些接口?
325+
326+
- Deque接口
327+
- BlockingQueue接口
328+
329+
### 7.5 哪些类实现了BlockDeque接口?
330+
331+
- LinkedBlockingDeque接口
332+
333+
## 八、TransferQueue接口
334+
335+
## 九、PriorityQueue类
336+
337+
## 十、ConcurrentLinkedQueue
338+
339+
## 十一、LinkedList类
340+
341+
## 十二、ArrayDeque类
342+
343+
## 十三、ConcurrentLinkedDeque
258344

259-
## 四、ArrayBlockingQueue
345+
## 十四、ArrayBlockingQueue
260346

261-
## 五、LinkedBlockingQueue
347+
## 十五、LinkedBlockingQueue
262348

263-
## 六、LinkedBlockingDeque
349+
## 十六、LinkedBlockingDeque
264350

265-
## 七、LinkedTransferQueue
351+
## 十七、LinkedTransferQueue
266352

267353

268354

Collapse file

‎docs/10.并发多线程/Balsamiq/队列.bmpr‎

Copy file name to clipboard
57 KB
Binary file not shown.

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.