<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Java `ArrayBlockingQueue` > 原文: [https://www.programiz.com/java-programming/arrayblockingqueue](https://www.programiz.com/java-programming/arrayblockingqueue) #### 在本教程中,我們將借助示例學習`ArrayBlockingQueue`類及其方法。 Java 集合框架的`ArrayBlockingQueue`類使用數組提供阻塞隊列實現。 它實現了 [Java `BlockingQueue`接口](/java-programming/blockingqueue "Java BlockingQueue Interface")。 ![ArrayBlockingQueue implements the BlockingQueue interface in Java.](https://img.kancloud.cn/1b/84/1b84b8c3170926b78c5e39876e32f48b_1028x600.png) * * * ## 創建`ArrayBlockingQueue` 為了創建一個數組阻塞隊列,我們??必須導入`java.util.concurrent.ArrayBlockingQueue`包。 導入包后,可以使用以下方法在 Java 中創建數組阻塞隊列: ```java ArrayBlockingQueue<Type> animal = new ArrayBlockingQueue<>(int capacity); ``` 這里, * `Type` - 數組阻塞隊列的類型 * `capcity` - 數組阻塞隊列的大小 例如, ```java // Creating String type ArrayBlockingQueue with size 5 ArrayBlockingQueue<String> animals = new ArrayBlockingQueue<>(5); // Creating Integer type ArrayBlockingQueue with size 5 ArrayBlockingQueue<Integer> age = new ArrayBlockingQueue<>(5); ``` **注意**:必須提供數組的大小。 * * * ## `ArrayBlockingQueue`的方法 `ArrayBlockingQueue`類提供`BlockingQueue`接口中所有方法的實現。 這些方法用于從數組阻塞隊列中插入,訪問和刪除元素。 另外,我們還將學習兩種支持數組阻塞隊列中阻塞操作的方法`put()`和`take()`。 這兩種方法將數組阻塞隊列與其他典型隊列區分開。 * * * ### 插入元素 * `add()` - 將指定的元素插入數組阻塞隊列。 如果隊列已滿,它將引發異常。 * `offer()` - 將指定的元素插入數組阻塞隊列。 如果隊列已滿,則返回`false`。 例如: ```java import java.util.concurrent.ArrayBlockingQueue; class Main { public static void main(String[] args) { ArrayBlockingQueue<String> animals = new ArrayBlockingQueue<>(5); // Using add() animals.add("Dog"); animals.add("Cat"); // Using offer() animals.offer("Horse"); System.out.println("ArrayBlockingQueue: " + animals); } } ``` **輸出** ```java ArrayBlockingQueue: [Dog, Cat, Horse] ``` * * * ### 訪問元素 * `peek()` - 從數組阻塞隊列的前面返回一個元素。 如果隊列為空,則返回`null`。 * `iterator()` - 返回一個迭代器對象,以順序訪問數組阻塞隊列中的元素。 如果隊列為空,則拋出異常。 我們必須導入`java.util.Iterator`包才能使用它。 例如: ```java import java.util.concurrent.ArrayBlockingQueue; import java.util.Iterator; class Main { public static void main(String[] args) { ArrayBlockingQueue<String> animals = new ArrayBlockingQueue<>(5); // Add elements animals.add("Dog"); animals.add("Cat"); animals.add("Horse"); System.out.println("ArrayBlockingQueue: " + animals); // Using peek() String element = animals.peek(); System.out.println("Accessed Element: " + element); // Using iterator() Iterator<String> iterate = animals.iterator(); System.out.print("ArrayBlockingQueue Elements: "); while(iterate.hasNext()) { System.out.print(iterate.next()); System.out.print(", "); } } } ``` **輸出**: ```java ArrayBlockingQueue: [Dog, Cat, Horse] Accessed Element: Dog ArrayBlockingQueue Elements: Dog, Cat, Horse, ``` * * * ### 刪除元素 * `remove()` - 返回并從數組阻塞隊列中刪除指定的元素。 如果隊列為空,則拋出異常。 * `poll()` - 返回并從數組阻塞隊列中刪除指定的元素。 如果隊列為空,則返回`null`。 * `clear()` - 從數組阻塞隊列中刪除所有元素。 例如: ```java import java.util.concurrent.ArrayBlockingQueue; class Main { public static void main(String[] args) { ArrayBlockingQueue<String> animals = new ArrayBlockingQueue<>(5); animals.add("Dog"); animals.add("Cat"); animals.add("Horse"); System.out.println("ArrayBlockingQueue: " + animals); // Using remove() String element1 = animals.remove(); System.out.println("Removed Element:"); System.out.println("Using remove(): " + element1); // Using poll() String element2 = animals.poll(); System.out.println("Using poll(): " + element2); // Using clear() animals.clear(); System.out.println("Updated ArrayBlockingQueue: " + animals); } } ``` **輸出**: ```java ArrayBlockingQueue: [Dog, Cat, Horse] Removed Elements: Using remove(): Dog Using poll(): Cat Updated ArrayBlockingQueue: [] ``` * * * ## `put()`和`take()`方法 在多線程進程中,我們可以使用`put()`和`take()`阻止一個線程的操作,使其與另一個線程同步。 這些方法將等待直到可以成功執行。 * * * ### `put()`方法 要將元素添加到數組阻塞隊列的末尾,可以使用`put()`方法。 如果數組阻塞隊列已滿,它將等待直到數組阻塞隊列中有空間添加元素。 例如: ```java import java.util.concurrent.ArrayBlockingQueue; class Main { public static void main(String[] args) { ArrayBlockingQueue<String> animals = new ArrayBlockingQueue<>(5); try { // Add elements to animals animals.put("Dog"); animals.put("Cat"); System.out.println("ArrayBlockingQueue: " + animals); } catch(Exception e) { System.out.println(e); } } } ``` **輸出**: ```java ArrayBlockingQueue: [Dog, Cat] ``` 在此,如果`InterruptedException`方法在等待時被中斷,則可能會拋出`InterruptedException`。 因此,我們必須將其封裝在`try..catch`塊內。 * * * ### `take()`方法 要從數組阻塞隊列的前面返回并刪除一個元素,我們可以使用`take()`方法。 如果數組阻塞隊列為空,它將等待,直到數組阻塞隊列中有要刪除的元素為止。 例如: ```java import java.util.concurrent.ArrayBlockingQueue; class Main { public static void main(String[] args) { ArrayBlockingQueue<String> animals = new ArrayBlockingQueue<>(5); try { //Add elements to animals animals.put("Dog"); animals.put("Cat"); System.out.println("ArrayBlockingQueue: " + animals); // Remove an element String element = animals.take(); System.out.println("Removed Element: " + element); } catch(Exception e) { System.out.println(e); } } } ``` **輸出**: ```java ArrayBlockingQueue: [Dog, Cat] Removed Element: Dog ``` 在這里,如果`take()`方法在等待時被中斷,則會拋出`InterrupedException`。 因此,我們必須將其封裝在[H??TG2]塊內。 * * * ## 其他方法 | 方法 | 內容描述 | | --- | --- | | `contains(element)` | 在數組阻塞隊列中搜索指定的元素。 如果找到該元素,則返回`true`,否則返回`false`。 | | `size()` | 返回數組阻塞隊列的長度。 | | `toArray()` | 將數組阻塞隊列轉換為數組并返回它。 | | `toString()` | 將數組阻塞隊列轉換為字符串 | * * * ## 為什么要使用`ArrayBlockingQueue`? `ArrayBlockingQueue`使用數組作為其內部存儲。 它被視為**線程安全的**集合。 因此,它通常用于多線程應用中。 假設一個線程正在將元素插入隊列,而另一個線程正在從隊列中刪除元素。 現在,如果第一個線程比第二個線程慢,那么數組阻塞隊列可使第二個線程等待,直到第一個線程完成其操作。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看