@@ -33,9 +33,9 @@ ArrayBlockingQueue 默认不保证线程访问的公平性 |
33 | 33 |
|
34 | 34 | ## 一、队列自我介绍 |
35 | 35 |
|
36 | | - |
| 36 | + |
37 | 37 |
|
38 | | -我叫队列,无论现实生活中还是计算机的世界中,我都是一个很重要的角色哦~ |
| 38 | +hi,大家好,我叫队列,无论现实生活中还是计算机的世界中,我都是一个很重要的角色哦~ |
39 | 39 |
|
40 | 40 | 我是一种数据结构,大家可以把我想象成一个数组,元素从我的一头进入、从另外一头出去,称为FIFO原则(先进先出原则)。 |
41 | 41 |
|
@@ -57,12 +57,13 @@ ArrayBlockingQueue 默认不保证线程访问的公平性 |
57 | 57 |
|
58 | 58 | 弄清楚下面的继承实现关系对后面理解18种队列有很大帮助。 |
59 | 59 |
|
60 | | - |
| 60 | + |
61 | 61 |
|
62 | 62 | - `Queue`接口**继承**`Collection`接口,`Collection`接口**继承** `Iterable`接口 |
63 | 63 | - `BlockingQueue`接口、`Deque`接口 **继承** `Queue`接口 |
64 | 64 | - `AbstractQueue`抽象类**实现**`Queue`接口 |
65 | 65 | - `BlockingDeque`接口、`TransferQueue`接口**继承**`BlockingQueue`接口 |
| 66 | +- `BlockingDeque`接口继承`Deque`接口 |
66 | 67 | - `LinkedBlockingDeque`类**实现**`BlockingDeque`接口 |
67 | 68 | - `LinkedTransferQueue`类接口**实现**`TransferQueue`接口 |
68 | 69 | - `LinkedList`类、`ArrayDeque`类、`ConcurrentLinkedDeque`类**实现** 了`Deque`接口 |
@@ -76,15 +77,15 @@ ArrayBlockingQueue 默认不保证线程访问的公平性 |
76 | 77 | - 接口继承接口,用 extends |
77 | 78 | - 类继承类,用extends |
78 | 79 |
|
79 | | -## 三、庖丁解牛Queue接口 |
| 80 | +## 三、万物归宗Queue接口 |
80 | 81 |
|
81 | 82 | ### 2.1 深入理解Queue接口的本质 |
82 | 83 |
|
83 | 84 | - Queue接口是一种Collection,被设计用于处理之前临时保存在某处的元素。 |
84 | 85 |
|
85 | 86 | - 除了基本的Collection操作之外,队列还提供了额外的插入、提取和检查操作。每一种操作都有两种形式:如果操作失败,则抛出一个异常;如果操作失败,则返回一个特殊值(null或false,取决于是什么操作)。 |
86 | 87 |
|
87 | | -- 队列通常是以FIFO(先进先出)的方式排序元素,但是不是必须的。 |
| 88 | +- 队列通常是以FIFO(先进先出)的方式排序元素,但是这不是必须的。 |
88 | 89 |
|
89 | 90 | - 只有优先级队列可以根据提供的比较器对元素进行排序或者是采用正常的排序。无论怎么排序,队列的头将通过调用remove()或poll()方法进行移除。在FIFO队列种,所有新的元素被插入到队尾。其他种类的队列可能使用不同的布局来存放元素。 |
90 | 91 | - 每个Queue必须指定排序属性。 |
@@ -112,7 +113,9 @@ ArrayBlockingQueue 默认不保证线程访问的公平性 |
112 | 113 |
|
113 | 114 | ## 四、双端队列Deque接口 |
114 | 115 |
|
115 | | - |
| 116 | +### 4.1 深入理解Deque接口的原理 |
| 117 | + |
| 118 | + |
116 | 119 |
|
117 | 120 | **(1)Deque概念:**支持两端元素插入和移除的线性集合。名称`deque`是双端队列的缩写,通常发音为`deck`。大多数实现Deque的类,对它们包含的元素的数量没有固定的限制的,支持有界和无界。 |
118 | 121 |
|
@@ -143,7 +146,20 @@ ArrayBlockingQueue 默认不保证线程访问的公平性 |
143 | 146 |
|
144 | 147 |  |
145 | 148 |
|
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抽象类 |
147 | 163 |
|
148 | 164 | AbstractQueue是一个抽象类,继承了Queue接口,提供了一些Queue操作的骨架实现。 |
149 | 165 |
|
@@ -186,11 +202,16 @@ public E element() { |
186 | 202 | } |
187 | 203 | ``` |
188 | 204 |
|
189 | | -如果继承抽象类则必须保证offer方法不允许null值插入。 |
| 205 | +注意: |
190 | 206 |
|
| 207 | +- 如果继承抽象类则必须保证offer方法不允许null值插入。 |
191 | 208 |
|
| 209 | +### 5.2 哪些类继承了AbstractQueue抽象类 |
192 | 210 |
|
193 | | -## 四、BlockingQueue |
| 211 | +- `ArrayBlockingQueue`类、`LinkendBlockingQueue`类、`LinkedBlockingDeque`类、`LinkedTransferQueue`类、`SynchronousQueue`类、`PriorityBlockQueue`类、`DelayQueue类`**继承** 了`AbstractQueue`抽象类 |
| 212 | +- `PriorityQueue`类和`ConcurrentLinkedQueue`类**继承** 了`AbstractQueue`抽象类 |
| 213 | + |
| 214 | +## 六、阻塞队列BlockingQueue |
194 | 215 |
|
195 | 216 | ### 3.1 宏观来看BlockingQueue(阻塞队列) |
196 | 217 |
|
@@ -208,16 +229,6 @@ public E element() { |
208 | 229 |
|
209 | 230 | (6)为什么要用阻塞队列:生产者生产和消费者消费的速率不一样,需要用队列来解决速率差问题,当队列满了或空的时候,则需要阻塞生产或消费动作来解决队列满或空的问题。 |
210 | 231 |
|
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 | | - |
221 | 232 | ### 3.2 案例解析 |
222 | 233 |
|
223 | 234 | 线程A往阻塞队列(Blocking Queue)中`添加`元素,而线程B从阻塞队列中`移除`元素。 |
@@ -231,38 +242,113 @@ public E element() { |
231 | 242 |
|
232 | 243 | ### 3.3 操刀BlockingQueue接口 |
233 | 244 |
|
234 | | -**BlockingQueue接口的12个核心方法:** |
| 245 | +**BlockingQueue接口的10个核心方法:** |
| 246 | + |
| 247 | + |
235 | 248 |
|
236 | | - |
| 249 | +**10个核心方法总结如下:** |
237 | 250 |
|
238 | | -有三大类操作:插入、移除、检查 |
| 251 | + |
239 | 252 |
|
240 | | -- 插入有四种方法:add、offer、put、offer超时版。 |
| 253 | +有三大类操作:插入、移除、检查。 |
| 254 | + |
| 255 | +- **插入有四种方法:** add、offer、put、offer超时版。 |
241 | 256 | - add方法特别之处用于添加失败时抛出异常,共有四种异常: |
242 | | - - IllegalStateException - 队列满了 |
243 | | - - ClassCastException - 添加的元素类型不匹配 |
244 | | - - NullPointerException - 添加的元素为null |
245 | | - - IllegalArgumentException - 添加的元素某些属性不匹配 |
| 257 | + - `IllegalStateException` - 队列满了 |
| 258 | + - `ClassCastException` - 添加的元素类型不匹配 |
| 259 | + - `NullPointerException` - 添加的元素为null |
| 260 | + - `IllegalArgumentException` - 添加的元素某些属性不匹配 |
246 | 261 | - offer方法特别之处用于添加失败时只返回false |
247 | 262 | - put方法特别之处用于当阻塞队列满时,生产者如果往队列里put元素,则队列会一直阻塞生产者线程,直到队列可用或者响应中断退出 |
248 | 263 | - offer超时方法特别之处用于当阻塞队列满时,生产者如果往队列里面插入元素,队列会阻塞生产者线程一段时间,如果超过了指定时间,生产者线程会退出,并返回false。 |
249 | | -- 移除有四种方法:remove、poll、take、poll超时版 |
| 264 | +- **移除有四种方法:** remove、poll、take、poll超时版 |
250 | 265 | - remove方法特别之处用于移除失败时抛出异常 |
251 | | - - NoSuchElementException - 如果这个队列是空的 |
| 266 | + - `NoSuchElementException` - 如果这个队列是空的 |
252 | 267 | - poll方法特别之处用于移除失败时返回null |
253 | 268 | - take方法特别之处用于当阻塞队列为空时,消费者线程如果从队列里面移除元素,则队列会一直阻塞消费者线程,直到队列不为空 |
254 | 269 | - poll超时方法特别之处用于当阻塞队列空时,消费者如果从队列里面删除元素,则队列会一直阻塞消费者线程,如果超过了指定时间,消费者线程会退出,并返回null |
255 | | -- 检查有两种方法:element、peek |
| 270 | +- **检查有两种方法:** element、peek |
256 | 271 | - 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 | + |
| 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 | + |
| 313 | + |
| 314 | +### 7.2 BlockDeque和BlockQueue的对等方法 |
| 315 | + |
| 316 | + |
| 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 |
258 | 344 |
|
259 | | -## 四、ArrayBlockingQueue |
| 345 | +## 十四、ArrayBlockingQueue |
260 | 346 |
|
261 | | -## 五、LinkedBlockingQueue |
| 347 | +## 十五、LinkedBlockingQueue |
262 | 348 |
|
263 | | -## 六、LinkedBlockingDeque |
| 349 | +## 十六、LinkedBlockingDeque |
264 | 350 |
|
265 | | -## 七、LinkedTransferQueue |
| 351 | +## 十七、LinkedTransferQueue |
266 | 352 |
|
267 | 353 |
|
268 | 354 |
|
|