Java ArrayBlockingQueue(int capacity) Constructor

Syntax

ArrayBlockingQueue(int capacity) constructor from ArrayBlockingQueue has the following syntax.

public ArrayBlockingQueue(int capacity)

Example

In the following code shows how to use ArrayBlockingQueue.ArrayBlockingQueue(int capacity) constructor.


import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
/*w  w w  .  j a va  2s . c  o  m*/
public class Main {
  public static void main(String[] argv) throws Exception {
    int capacity = 10;
    BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity);

    int numWorkers = 2;
    Worker[] workers = new Worker[numWorkers];
    for (int i = 0; i < workers.length; i++) {
      workers[i] = new Worker(queue);
      workers[i].start();
    }

    for (int i = 0; i < 100; i++) {
      queue.put(i);
    }
  }
}

class Worker extends Thread {
  BlockingQueue<Integer> q;

  Worker(BlockingQueue<Integer> q) {
    this.q = q;
  }

  public void run() {
    try {
      while (true) {
        Integer x = q.take();
        if (x == null) {
          break;
        }
        System.out.println(x);
      }
    } catch (InterruptedException e) {
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    java.util.concurrent »




ArrayBlockingQueue
BlockingDeque
CountDownLatch
CyclicBarrier
TimeUnit