Skip to content

Commit 32397ff

Browse files
author
Justin Lin
committed
格式修正
1 parent b695bc6 commit 32397ff

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

docs/CH15.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,18 +1141,18 @@ class TestThread extends Thread {
11411141

11421142
BlockingQueue 的幾個主要操作為下:
11431143

1144-
#### **7.1 BlockingQueue 的幾個操作**
1145-
1146-
| 方法 | 說明
1147-
|:-- |
1148-
| add() | 加入元素,如果佇列是滿的,則丟出 IllegalStateException
1149-
| remove() | 傳回並從佇列移除元素,如果佇列是空的,則丟出 NoSuchElementException
1150-
| element() | 傳回元素,如果佇列是空的,則丟出 NoSuchElementException
1151-
| offer() | 加入元素並傳回 true,如果佇列是滿的,則傳回 false
1152-
| poll() | 傳回並從佇列移除元素,如果佇列是空的,則傳回 null
1153-
| peek() | 傳回元素,如果佇列是空的,則傳回 null
1154-
| put() | 加入元素,如果佇列是滿,就 block
1155-
| take() | 傳回並移除元素,如果佇列是空的,就 block
1144+
#### **15.1 BlockingQueue 的幾個操作**
1145+
1146+
| 方法 | 說明 |
1147+
|:--- | :--- |
1148+
| add() | 加入元素,如果佇列是滿的,則丟出 IllegalStateException |
1149+
| remove() | 傳回並從佇列移除元素,如果佇列是空的,則丟出 NoSuchElementException |
1150+
| element() | 傳回元素,如果佇列是空的,則丟出 NoSuchElementException |
1151+
| offer() | 加入元素並傳回 true,如果佇列是滿的,則傳回 false |
1152+
| poll() | 傳回並從佇列移除元素,如果佇列是空的,則傳回 null |
1153+
| peek() | 傳回元素,如果佇列是空的,則傳回 null |
1154+
| put() | 加入元素,如果佇列是滿,就 block |
1155+
| take() | 傳回並移除元素,如果佇列是空的,就 block |
11561156

11571157
在 java.util.concurrent 下提供幾種不同的 BlockingQueue,ArrayBlockingQueue 要指定容量大小來建構。LinkedBlockingQueue 預設沒有容量上限,但也可以指定容量上限。PriorityBlockingQueue 嚴格來說不是 Queue,因為它是根據優先權(Priority)來移除元素。
11581158

@@ -1244,6 +1244,7 @@ public class ConsumerQueue implements Runnable {
12441244
}
12451245
}
12461246
}
1247+
12471248
```
12481249

12491250
由於 BlockingQueue 不再是由您來控制了,所以在這邊就沒有特意顯示訊息以表示生產者、消費者放入產品至 Queue 的訊息,不過您仍可以在 ProducerQueue 與 ConsumerQueue 中放入相關訊息顯示,以確認程式確實有在運作。
@@ -1367,10 +1368,10 @@ java.util.concurrent.FutureTask 是個代理,真正執行找質數的是 Calla
13671368
有時候您需要建立一堆執行緒來執行一些小任務,然而頻繁的建立執行緒有時會是個開銷,因為執行緒的建立必須與作業系統互動,如果能建立一個執行緒池(Thread pool)來管理這些小的執行緒並加以重複使用,對於系統效能會是個改善的方式。
13681369
您可以使用 Executors 來建立執行緒池,Executors 有幾個靜態(static)方法,列出如下:
13691370

1370-
#### **7.2 Executors 幾個靜態方法**
1371+
#### **15.2 Executors 幾個靜態方法**
13711372

1372-
| 方法 | 說明
1373-
|:--- |
1373+
| 方法 | 說明
1374+
| :--- | :---
13741375
| newCachedThreadPool() | 建立可以快取的執行緒,每個執行緒預設可idle的時間為60秒
13751376
| newFixedThreadPool() | 包括固定數量的執行緒
13761377
| newSingleThreadExecutor() | 只有一個執行緒,循序的執行指定給它的每個任務

0 commit comments

Comments
 (0)