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@447c4d2

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
    • 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
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public static void main(String[] args) {
2525

2626
queue.add(100);
2727
queue.add(200);
28+
29+
//TransferQueue;
2830
//int a = queue.peek();
2931
// PriorityQueue;
3032
// Deque;
Collapse file

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

Copy file name to clipboardExpand all lines: docs/10.并发多线程/06.图解队列.md
+55-15Lines changed: 55 additions & 15 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ hi,大家好,我叫队列,无论现实生活中还是计算机的世界中
111111
- (4)Queue接口没有定义阻塞队列的方法,这些方法在BlockQueue接口中定义了。
112112
- (5)Queue实现类通常不允许插入null元素,尽管一些实现类比如LinkedList不禁止插入null,但是还是不建议插入null,因为null也被用在poll方法的特殊返回值,以说明队列不包含元素。
113113

114-
## 四、双端队列Deque接口
114+
## 四、双端可用Deque接口
115115

116116
### 4.1 深入理解Deque接口的原理
117117

@@ -157,7 +157,7 @@ hi,大家好,我叫队列,无论现实生活中还是计算机的世界中
157157

158158
- BlockingDeque接口
159159

160-
## 五、队列骨架 AbstractQueue
160+
## 五、队列骨架AbstractQueue抽象类
161161

162162
### 5.1 深入理解AbstractQueue抽象类
163163

@@ -211,7 +211,7 @@ public E element() {
211211
- `ArrayBlockingQueue`类、`LinkendBlockingQueue`类、`LinkedBlockingDeque`类、`LinkedTransferQueue`类、`SynchronousQueue`类、`PriorityBlockQueue`类、`DelayQueue类`**继承** 了`AbstractQueue`抽象类
212212
- `PriorityQueue`类和`ConcurrentLinkedQueue`类**继承** 了`AbstractQueue`抽象类
213213

214-
## 六、阻塞队列BlockingQueue
214+
## 六、阻塞缓冲BlockingQueue接口
215215

216216
### 3.1 宏观来看BlockingQueue(阻塞队列)
217217

@@ -279,18 +279,18 @@ public E element() {
279279
### 3.5 哪些类实现了BlockingQueue接口?
280280

281281
- ArrayBlockingQueue类 - 由数组构成的有界阻塞队列
282-
- LinkedBlockingQueue类 - 由链表构成的有界阻塞队列,界限默认大小为Integer.MAX_Value(21亿),值非常大,相当于无界。
282+
- LinkedBlockingQueue类 - 由链表构成的有界阻塞队列,界限默认大小为Integer.MAX_Value(2^31-1),值非常大,相当于无界。
283283
- LinkedBlockingDeque类 - 由链表构成的双向阻塞队列
284284
- LinkedTransferQueue类 - 由链表构成的无界阻塞队列
285285
- SynchronousQueue类 - 不存储元素的阻塞队列,只有一个元素进行数据传递。
286286
- LinkedTransferQueue类 - 由链表构成的无界阻塞TransferQueue队列
287287
- DelayQueue类 - 使用优先级队列实现的延迟无界阻塞队列
288288

289-
## 七、BlockingDeque接口
289+
## 七、双端阻塞BlockingDeque接口
290290

291291
### 7.1 BlockingDeque接口方法
292292

293-
是阻塞和双向队列接口的结合。有如下方法:
293+
是阻塞队列`BlockingQueue`和双向队列`Deque`接口的结合。有如下方法:
294294

295295
![mark](http://cdn.jayh.club/blog/20200907/Ar0RX1xQKRB7.png?imageslim)
296296

@@ -307,7 +307,7 @@ queue.addLast("400");
307307

308308
最后队列中的元素顺序如下:
309309

310-
300, test1, 400
310+
300, test1, 400。先添加了test1放到队列的头部,然后在头部的前面放入300,所以300在最前面,成为头部,然后将400放入队列的尾部,所以最后的结果是300, test1, 400。
311311

312312
![mark](http://cdn.jayh.club/blog/20200907/xyU7h59rcTVc.png?imageslim)
313313

@@ -330,25 +330,65 @@ queue.addLast("400");
330330

331331
- LinkedBlockingDeque接口
332332

333-
## 八、TransferQueue接口
333+
## 八、使命必达TransferQueue接口
334334

335-
## 九、PriorityQueue类
335+
### 8.1 Transfer怎么理解?
336336

337-
## 十、ConcurrentLinkedQueue
337+
如果有消费者正在获取元素,则将队列中的元素传递给消费者。如果没有消费者,则等待消费者消费。我把它称作使命必达队列,必须将任务完成才能返回。
338+
339+
### 8.2 TransferQueue接口比其他阻塞队列多了5个方法
340+
341+
- `transfer(E e)`
342+
343+
原理如下图所示:
344+
345+
![transfer方法的原理](http://cdn.jayh.club/blog/20200907/Tpbohg41zePQ.png?imageslim)
346+
347+
- 原理图解释:生产者线程Producer Thread尝试将元素B传给消费者线程,如果没有消费者线程,则将元素B放到尾节点。并且生产者线程等待元素B被消费。当元素B被消费后,生产者线程返回。
348+
- 如果当前有消费者正在等待接收元素(消费者通过take方法或超时限制的poll方法时),transfer方法可以把生产者传入的元素立刻transfer(传输)给消费者。
349+
- 如果没有消费者等待接收元素,transfer方法会将元素放在队列的tail(尾)节点,并等到该元素被消费者消费了才返回。
350+
351+
- `tryTransfer(E e)`
352+
353+
- 试探生产者传入的元素是否能直接传给消费者。
354+
- 如果没有消费者等待接收元素,则返回false。
355+
- 和transfer方法的区别是,无论消费者是否接收,方法立即返回。
356+
357+
- `tryTransfer(E e, long timeout, TimeUnit unit)`
358+
359+
- 带有时间限制的tryTransfer方法。
360+
- 试图把生产者传入的元素直接传给消费者。
361+
- 如果没有消费者消费该元素则等待指定的时间再返回。
362+
- 如果超时了还没有消费元素,则返回false。
363+
- 如果在超时时间内消费了元素,则返回true。
364+
365+
- `getWaitingConsumerCount()`
366+
367+
- 获取通过BlockingQueue.take()方法或超时限制poll方法等待接受元素的消费者数量。近似值。
368+
- 返回等待接收元素的消费者数量。
369+
370+
- `hasWaitingConsumer()`
371+
372+
- 获取是否有通过BlockingQueue.tabke()方法或超时限制poll方法等待接受元素的消费者。
373+
- 返回true则表示至少有一个等待消费者。
374+
375+
## 九、优先由你PriorityQueue类
376+
377+
## 十、ConcurrentLinkedQueue类
338378

339379
## 十一、LinkedList类
340380

341381
## 十二、ArrayDeque类
342382

343-
## 十三、ConcurrentLinkedDeque
383+
## 十三、ConcurrentLinkedDeque类
344384

345-
## 十四、ArrayBlockingQueue
385+
## 十四、ArrayBlockingQueue类
346386

347-
## 十五、LinkedBlockingQueue
387+
## 十五、传球手SynchronousQueue类
348388

349-
## 十六、LinkedBlockingDeque
389+
## 十六、PriorityBlockQueue类
350390

351-
## 十七、LinkedTransferQueue
391+
## 十七、DelayQueue类
352392

353393

354394

Collapse file

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

Copy file name to clipboard
15 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.