Java : ArrayBlockingQueue (ブロッキング・配列キュー) - API使用例
ArrayBlockingQueue (Java SE 20 & JDK 20) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
ArrayBlockingQueue は、
- 要素を取り出すときに、もし空だったら要素が追加されるまで待機
という操作が可能な BlockingQueue の実装です。
要素の管理には配列が使われます。
// 基準となる時刻
final long current = System.nanoTime();
// 基準となる時刻からの差分を秒として取得
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;
try (final var executor = Executors.newSingleThreadExecutor()) {
final var queue = new ArrayBlockingQueue<String>(10);
final var future = executor.submit(() -> {
try {
while (true) {
System.out.println(" take ...");
final var value = queue.take();
System.out.printf(" take OK! : value = %s (%f sec.)%n",
value, elapsedTime.getAsDouble());
}
} catch (InterruptedException e) {
System.out.println(" InterruptedException!");
}
});
System.out.printf("put values (%f sec.)%n", elapsedTime.getAsDouble());
queue.put("aaa");
queue.put("bbb");
queue.put("ccc");
TimeUnit.SECONDS.sleep(5);
System.out.printf("put values (%f sec.)%n", elapsedTime.getAsDouble());
queue.put("XXX");
queue.put("YYY");
queue.put("ZZZ");
TimeUnit.SECONDS.sleep(5);
System.out.println("future.cancel");
future.cancel(true);
}
// 結果
// ↓
//put values (0.003381 sec.)
// take ...
// take OK! : value = aaa (0.004735 sec.)
// take ...
// take OK! : value = bbb (0.004861 sec.)
// take ...
// take OK! : value = ccc (0.004984 sec.)
// take ...
//put values (5.016605 sec.)
// take OK! : value = XXX (5.017193 sec.)
// take ...
// take OK! : value = YYY (5.017474 sec.)
// take ...
// take OK! : value = ZZZ (5.017649 sec.)
// take ...
//future.cancel
// InterruptedException!
コンストラクタ
ArrayBlockingQueue (int capacity)
final var queue = new ArrayBlockingQueue<String>(3);
System.out.println(queue); // []
System.out.println(queue.size()); // 0
System.out.println(queue.remainingCapacity()); // 3
System.out.println(queue.offer("aaa")); // true
System.out.println(queue); // [aaa]
System.out.println(queue.size()); // 1
System.out.println(queue.remainingCapacity()); // 2
System.out.println(queue.offer("bbb")); // true
System.out.println(queue); // [aaa, bbb]
System.out.println(queue.size()); // 2
System.out.println(queue.remainingCapacity()); // 1
System.out.println(queue.offer("ccc")); // true
System.out.println(queue); // [aaa, bbb, ccc]
System.out.println(queue.size()); // 3
System.out.println(queue.remainingCapacity()); // 0
System.out.println(queue.offer("XXX")); // false
System.out.println(queue); // [aaa, bbb, ccc]
System.out.println(queue.size()); // 3
System.out.println(queue.remainingCapacity()); // 0
System.out.println(queue.poll()); // aaa
System.out.println(queue); // [bbb, ccc]
System.out.println(queue.size()); // 2
System.out.println(queue.remainingCapacity()); // 1
System.out.println(queue.offer("YYY")); // true
System.out.println(queue); // [bbb, ccc, YYY]
System.out.println(queue.size()); // 3
System.out.println(queue.remainingCapacity()); // 0
ArrayBlockingQueue (int capacity, boolean fair)
capacity パラメータについては ArrayBlockingQueue(int capacity) の使用例をご参照ください。
void testFair(boolean fair) throws InterruptedException {
System.out.println("fair = " + fair);
final var queue = new ArrayBlockingQueue<Integer>(3, fair);
queue.put(0);
queue.put(0);
queue.put(0);
System.out.println("before = " + queue);
try (final var executor = Executors.newFixedThreadPool(2)) {
executor.submit(() -> {
try {
queue.put(1);
queue.poll();
queue.put(2);
queue.poll();
} catch (InterruptedException e) {
System.out.println("InterruptedException!");
}
});
TimeUnit.MILLISECONDS.sleep(100);
executor.submit(() -> {
try {
queue.put(3);
queue.poll();
} catch (InterruptedException e) {
System.out.println("InterruptedException!");
}
});
TimeUnit.MILLISECONDS.sleep(100);
queue.poll();
}
System.out.println("after = " + queue);
}
testFair(true);
// 結果
// ↓
//fair = true
//before = [0, 0, 0]
//after = [3, 2]
testFair(false);
// 結果
// ↓
//fair = false
//before = [0, 0, 0]
//after = [2, 3]
ArrayBlockingQueue (int capacity, boolean fair, Collection<? extends E> c)
c パラメータ以外については、下記の使用例もご参照ください。
- capacity パラメータ:ArrayBlockingQueue(int capacity)
- fair パラメータ:ArrayBlockingQueue(int capacity, boolean fair)
final var c = List.of("a", "b", "c");
final var queue = new ArrayBlockingQueue<>(10, false, c);
System.out.println(queue); // [a, b, c]
System.out.println(queue.size()); // 3
メソッド
boolean add (E e)
// capacity = 3
final var queue = new ArrayBlockingQueue<String>(3);
System.out.println(queue); // []
System.out.println(queue.add("aaa")); // true
System.out.println(queue); // [aaa]
System.out.println(queue.add("bbb")); // true
System.out.println(queue); // [aaa, bbb]
System.out.println(queue.add("ccc")); // true
System.out.println(queue); // [aaa, bbb, ccc]
try {
queue.add("ddd");
} catch (IllegalStateException e) {
System.out.println("IllegalStateException! : " + e.getMessage());
}
// 結果
// ↓
//IllegalStateException! : Queue full
void clear ()
final var queue = new ArrayBlockingQueue<String>(10);
System.out.println(queue); // []
System.out.println(queue.offer("a")); // true
System.out.println(queue); // [a]
System.out.println(queue.offer("b")); // true
System.out.println(queue); // [a, b]
queue.clear();
System.out.println(queue); // []
boolean contains (Object o)
final var queue = new ArrayBlockingQueue<String>(10);
System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("bbb")); // true
System.out.println(queue.offer("ccc")); // true
System.out.println(queue); // [aaa, bbb, ccc]
System.out.println(queue.contains("aaa")); // true
System.out.println(queue.contains("bbb")); // true
System.out.println(queue.contains("XXX")); // false
int drainTo (Collection<? super E> c)
final var queue = new ArrayBlockingQueue<String>(10);
final var c = new ArrayList<String>();
System.out.println(queue.offer("aaa")); // true
System.out.println(queue); // [aaa]
System.out.println(c); // []
System.out.println(queue.drainTo(c)); // 1
System.out.println(queue); // []
System.out.println(c); // [aaa]
System.out.println(queue.offer("bbb")); // true
System.out.println(queue.offer("ccc")); // true
System.out.println(queue); // [bbb, ccc]
System.out.println(c); // [aaa]
System.out.println(queue.drainTo(c)); // 2
System.out.println(queue); // []
System.out.println(c); // [aaa, bbb, ccc]
int drainTo (Collection<? super E> c, int maxElements)
final var queue = new ArrayBlockingQueue<String>(10);
final var c = new ArrayList<String>();
System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("bbb")); // true
System.out.println(queue.offer("ccc")); // true
System.out.println(queue); // [aaa, bbb, ccc]
System.out.println(c); // []
System.out.println(queue.drainTo(c, 10)); // 3
System.out.println(queue); // []
System.out.println(c); // [aaa, bbb, ccc]
System.out.println(queue.offer("XXX")); // true
System.out.println(queue.offer("YYY")); // true
System.out.println(queue.offer("ZZZ")); // true
System.out.println(queue); // [XXX, YYY, ZZZ]
System.out.println(c); // [aaa, bbb, ccc]
System.out.println(queue.drainTo(c, 2)); // 2
System.out.println(queue); // [ZZZ]
System.out.println(c); // [aaa, bbb, ccc, XXX, YYY]
void forEach (Consumer<? super E> action)
final var queue = new ArrayBlockingQueue<String>(10);
System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("bbb")); // true
System.out.println(queue.offer("ccc")); // true
System.out.println(queue); // [aaa, bbb, ccc]
System.out.println("-- forEach --");
queue.forEach(value -> {
System.out.println("value = " + value);
});
// 結果
// ↓
//-- forEach --
//value = aaa
//value = bbb
//value = ccc
Iterator<E> iterator ()
final var queue = new ArrayBlockingQueue<String>(10);
System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("bbb")); // true
System.out.println(queue.offer("ccc")); // true
System.out.println(queue); // [aaa, bbb, ccc]
final var iterator = queue.iterator();
System.out.println("-- forEachRemaining --");
iterator.forEachRemaining(value -> {
System.out.println("value = " + value);
});
// 結果
// ↓
//-- forEachRemaining --
//value = aaa
//value = bbb
//value = ccc
boolean offer (E e)
// capacity = 3
final var queue = new ArrayBlockingQueue<String>(3);
System.out.println(queue); // []
System.out.println(queue.offer("aaa")); // true
System.out.println(queue); // [aaa]
System.out.println(queue.offer("bbb")); // true
System.out.println(queue); // [aaa, bbb]
System.out.println(queue.offer("ccc")); // true
System.out.println(queue); // [aaa, bbb, ccc]
System.out.println(queue.offer("ddd")); // false
System.out.println(queue); // [aaa, bbb, ccc]
boolean offer (E e, long timeout, TimeUnit unit)
関連:offer(E e)
// 基準となる時刻
final long current = System.nanoTime();
// 基準となる時刻からの差分を秒として取得
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;
try (final var executor = Executors.newSingleThreadExecutor()) {
final var queue = new ArrayBlockingQueue<String>(3);
System.out.printf("remainingCapacity = %d (%f sec.)%n",
queue.remainingCapacity(), elapsedTime.getAsDouble());
executor.submit(() -> {
try {
final var list = List.of("aaa", "bbb", "ccc", "ddd");
for (final var value : list) {
System.out.println(" offer ...");
final var ret = queue.offer(value, 5, TimeUnit.SECONDS);
System.out.printf(" offer ret = %b (%f sec.)%n",
ret, elapsedTime.getAsDouble());
}
} catch (InterruptedException e) {
System.out.println(" InterruptedException!");
}
});
TimeUnit.SECONDS.sleep(10);
System.out.printf("queue = %s (%f sec.)%n", queue, elapsedTime.getAsDouble());
}
// 結果
// ↓
//remainingCapacity = 3 (0.002247 sec.)
// offer ...
// offer ret = true (0.005056 sec.)
// offer ...
// offer ret = true (0.005206 sec.)
// offer ...
// offer ret = true (0.005341 sec.)
// offer ...
// offer ret = false (5.007624 sec.)
//queue = [aaa, bbb, ccc] (10.013141 sec.)
E peek ()
final var queue = new ArrayBlockingQueue<String>(10);
System.out.println(queue); // []
System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("bbb")); // true
System.out.println(queue.offer("ccc")); // true
System.out.println(queue.peek()); // aaa
System.out.println(queue); // [aaa, bbb, ccc]
System.out.println(queue.poll()); // aaa
System.out.println(queue); // [bbb, ccc]
System.out.println(queue.peek()); // bbb
System.out.println(queue); // [bbb, ccc]
System.out.println(queue.poll()); // bbb
System.out.println(queue); // [ccc]
System.out.println(queue.peek()); // ccc
System.out.println(queue); // [ccc]
System.out.println(queue.poll()); // ccc
System.out.println(queue); // []
System.out.println(queue.peek()); // null
System.out.println(queue); // []
E poll ()
final var queue = new ArrayBlockingQueue<String>(10);
System.out.println(queue); // []
System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("bbb")); // true
System.out.println(queue.offer("ccc")); // true
System.out.println(queue); // [aaa, bbb, ccc]
System.out.println(queue.poll()); // aaa
System.out.println(queue); // [bbb, ccc]
System.out.println(queue.poll()); // bbb
System.out.println(queue); // [ccc]
System.out.println(queue.poll()); // ccc
System.out.println(queue); // []
System.out.println(queue.poll()); // null
System.out.println(queue); // []
E poll (long timeout, TimeUnit unit)
関連:poll()
// 基準となる時刻
final long current = System.nanoTime();
// 基準となる時刻からの差分を秒として取得
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;
try (final var executor = Executors.newSingleThreadExecutor()) {
final var queue = new ArrayBlockingQueue<String>(10);
final var future = executor.submit(() -> {
try {
while (true) {
System.out.println(" poll ...");
final var value = queue.poll(5, TimeUnit.SECONDS);
System.out.printf(" poll value = %s (%f sec.)%n",
value, elapsedTime.getAsDouble());
}
} catch (InterruptedException e) {
System.out.println(" InterruptedException!");
}
});
System.out.printf("put values (%f sec.)%n", elapsedTime.getAsDouble());
queue.put("aaa");
queue.put("bbb");
queue.put("ccc");
TimeUnit.SECONDS.sleep(8);
System.out.println("future.cancel");
future.cancel(true);
}
// 結果
// ↓
//put values (0.002562 sec.)
// poll ...
// poll value = aaa (0.003861 sec.)
// poll ...
// poll value = bbb (0.003984 sec.)
// poll ...
// poll value = ccc (0.004109 sec.)
// poll ...
// poll value = null (5.017120 sec.)
// poll ...
//future.cancel
// InterruptedException!
void put (E e)
// 基準となる時刻
final long current = System.nanoTime();
// 基準となる時刻からの差分を秒として取得
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;
try (final var executor = Executors.newSingleThreadExecutor()) {
final var queue = new ArrayBlockingQueue<String>(3);
System.out.printf("remainingCapacity = %d (%f sec.)%n",
queue.remainingCapacity(), elapsedTime.getAsDouble());
final var future = executor.submit(() -> {
try {
final var list = List.of("aaa", "bbb", "ccc", "ddd");
for (final var value : list) {
System.out.println(" put ...");
queue.put(value);
System.out.printf(" put OK! (%f sec.)%n", elapsedTime.getAsDouble());
}
} catch (InterruptedException e) {
System.out.printf(" InterruptedException! (%f sec.)%n", elapsedTime.getAsDouble());
}
});
TimeUnit.SECONDS.sleep(5);
System.out.printf("queue = %s (%f sec.)%n", queue, elapsedTime.getAsDouble());
System.out.println("future.cancel");
future.cancel(true);
}
// 結果
// ↓
//remainingCapacity = 3 (0.001621 sec.)
// put ...
// put OK! (0.007701 sec.)
// put ...
// put OK! (0.008398 sec.)
// put ...
// put OK! (0.008657 sec.)
// put ...
//queue = [aaa, bbb, ccc] (5.007803 sec.)
//future.cancel
// InterruptedException! (5.009321 sec.)
int remainingCapacity ()
final var queue = new ArrayBlockingQueue<String>(3);
System.out.println(queue); // []
System.out.println(queue.remainingCapacity()); // 3
System.out.println(queue.offer("aaa")); // true
System.out.println(queue); // [aaa]
System.out.println(queue.remainingCapacity()); // 2
System.out.println(queue.offer("bbb")); // true
System.out.println(queue); // [aaa, bbb]
System.out.println(queue.remainingCapacity()); // 1
System.out.println(queue.offer("ccc")); // true
System.out.println(queue); // [aaa, bbb, ccc]
System.out.println(queue.remainingCapacity()); // 0
queue.clear();
System.out.println(queue); // []
System.out.println(queue.remainingCapacity()); // 3
boolean remove (Object o)
final var queue = new ArrayBlockingQueue<String>(10);
System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("BBB")); // true
System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("BBB")); // true
System.out.println(queue.offer("ccc")); // true
System.out.println(queue); // [aaa, BBB, aaa, BBB, ccc]
System.out.println(queue.remove("aaa")); // true
System.out.println(queue); // [BBB, aaa, BBB, ccc]
System.out.println(queue.remove("BBB")); // true
System.out.println(queue); // [aaa, BBB, ccc]
System.out.println(queue.remove("BBB")); // true
System.out.println(queue); // [aaa, ccc]
System.out.println(queue.remove("XXX")); // false
System.out.println(queue); // [aaa, ccc]
boolean removeAll (Collection<?> c)
final var src = List.of("a", "b", "a", "b", "A", "B");
System.out.println(src); // [a, b, a, b, A, B]
{
final var queue = new ArrayBlockingQueue<>(10, false, src);
System.out.println(queue); // [a, b, a, b, A, B]
System.out.println(queue.removeAll(List.of())); // false
System.out.println(queue); // [a, b, a, b, A, B]
}
{
final var queue = new ArrayBlockingQueue<>(10, false, src);
System.out.println(queue); // [a, b, a, b, A, B]
System.out.println(queue.removeAll(List.of("a"))); // true
System.out.println(queue); // [b, b, A, B]
}
{
final var queue = new ArrayBlockingQueue<>(10, false, src);
System.out.println(queue); // [a, b, a, b, A, B]
System.out.println(queue.removeAll(List.of("a", "b"))); // true
System.out.println(queue); // [A, B]
}
{
final var queue = new ArrayBlockingQueue<>(10, false, src);
System.out.println(queue); // [a, b, a, b, A, B]
System.out.println(queue.removeAll(List.of("b", "a"))); // true
System.out.println(queue); // [A, B]
}
{
final var queue = new ArrayBlockingQueue<>(10, false, src);
System.out.println(queue); // [a, b, a, b, A, B]
System.out.println(queue.removeAll(List.of("A"))); // true
System.out.println(queue); // [a, b, a, b, B]
}
{
final var queue = new ArrayBlockingQueue<>(10, false, src);
System.out.println(queue); // [a, b, a, b, A, B]
System.out.println(queue.removeAll(List.of("X", "Y", "Z"))); // false
System.out.println(queue); // [a, b, a, b, A, B]
}
{
final var queue = new ArrayBlockingQueue<>(10, false, src);
System.out.println(queue); // [a, b, a, b, A, B]
System.out.println(queue.removeAll(List.of("A", "X", "Y", "Z"))); // true
System.out.println(queue); // [a, b, a, b, B]
}
boolean removeIf (Predicate<? super E> filter)
final var queue = new ArrayBlockingQueue<String>(10);
System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("BBB")); // true
System.out.println(queue.offer("ccc")); // true
System.out.println(queue.offer("DDD")); // true
System.out.println(queue); // [aaa, BBB, ccc, DDD]
final var ret = queue.removeIf(s -> {
// 大文字であるかチェックします。
return s.equals(s.toUpperCase());
});
System.out.println(ret); // true
System.out.println(queue); // [aaa, ccc]
final var queue = new ArrayBlockingQueue<String>(10);
System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("bbb")); // true
System.out.println(queue); // [aaa, bbb]
final var ret = queue.removeIf(s -> s.equals(s.toUpperCase()));
System.out.println(ret); // false
System.out.println(queue); // [aaa, bbb]
boolean retainAll (Collection<?> c)
final var src = List.of("a", "b", "a", "b", "A", "B");
System.out.println(src); // [a, b, a, b, A, B]
{
final var queue = new ArrayBlockingQueue<>(10, false, src);
System.out.println(queue); // [a, b, a, b, A, B]
System.out.println(queue.retainAll(List.of())); // true
System.out.println(queue); // []
}
{
final var queue = new ArrayBlockingQueue<>(10, false, src);
System.out.println(queue); // [a, b, a, b, A, B]
System.out.println(queue.retainAll(List.of("a", "b"))); // true
System.out.println(queue); // [a, b, a, b]
}
{
final var queue = new ArrayBlockingQueue<>(10, false, src);
System.out.println(queue); // [a, b, a, b, A, B]
System.out.println(queue.retainAll(List.of("b", "a"))); // true
System.out.println(queue); // [a, b, a, b]
}
{
final var queue = new ArrayBlockingQueue<>(10, false, src);
System.out.println(queue); // [a, b, a, b, A, B]
System.out.println(queue.retainAll(List.of("A"))); // true
System.out.println(queue); // [A]
}
{
final var queue = new ArrayBlockingQueue<>(10, false, src);
System.out.println(queue); // [a, b, a, b, A, B]
System.out.println(queue.retainAll(List.of("X", "Y", "Z"))); // true
System.out.println(queue); // []
}
{
final var queue = new ArrayBlockingQueue<>(10, false, src);
System.out.println(queue); // [a, b, a, b, A, B]
System.out.println(queue.retainAll(List.of("A", "X", "Y", "Z"))); // true
System.out.println(queue); // [A]
}
{
final var queue = new ArrayBlockingQueue<>(10, false, src);
System.out.println(queue); // [a, b, a, b, A, B]
System.out.println(queue.retainAll(List.of("a", "b", "A", "B"))); // false
System.out.println(queue); // [a, b, a, b, A, B]
}
int size ()
final var queue = new ArrayBlockingQueue<String>(3);
System.out.println(queue); // []
System.out.println(queue.size()); // 0
System.out.println(queue.offer("aaa")); // true
System.out.println(queue); // [aaa]
System.out.println(queue.size()); // 1
System.out.println(queue.offer("bbb")); // true
System.out.println(queue); // [aaa, bbb]
System.out.println(queue.size()); // 2
System.out.println(queue.offer("ccc")); // true
System.out.println(queue); // [aaa, bbb, ccc]
System.out.println(queue.size()); // 3
queue.clear();
System.out.println(queue); // []
System.out.println(queue.size()); // 0
Spliterator<E> spliterator ()
final var queue = new ArrayBlockingQueue<String>(10);
System.out.println(queue.offer("aaa")); // true
System.out.println(queue.offer("bbb")); // true
System.out.println(queue.offer("ccc")); // true
System.out.println(queue); // [aaa, bbb, ccc]
final var spliterator = queue.spliterator();
System.out.println("-- forEachRemaining --");
spliterator.forEachRemaining(value -> {
System.out.println("value = " + value);
});
// 結果
// ↓
//-- forEachRemaining --
//value = aaa
//value = bbb
//value = ccc
E take ()
// 基準となる時刻
final long current = System.nanoTime();
// 基準となる時刻からの差分を秒として取得
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;
try (final var executor = Executors.newSingleThreadExecutor()) {
final var queue = new ArrayBlockingQueue<String>(10);
final var future = executor.submit(() -> {
try {
while (true) {
System.out.println(" take ...");
final var value = queue.take();
System.out.printf(" take OK! : value = %s (%f sec.)%n",
value, elapsedTime.getAsDouble());
}
} catch (InterruptedException e) {
System.out.println(" InterruptedException!");
}
});
System.out.printf("put values (%f sec.)%n", elapsedTime.getAsDouble());
queue.put("aaa");
queue.put("bbb");
queue.put("ccc");
TimeUnit.SECONDS.sleep(5);
System.out.printf("put values (%f sec.)%n", elapsedTime.getAsDouble());
queue.put("XXX");
queue.put("YYY");
queue.put("ZZZ");
TimeUnit.SECONDS.sleep(5);
System.out.println("future.cancel");
future.cancel(true);
}
// 結果
// ↓
//put values (0.003381 sec.)
// take ...
// take OK! : value = aaa (0.004735 sec.)
// take ...
// take OK! : value = bbb (0.004861 sec.)
// take ...
// take OK! : value = ccc (0.004984 sec.)
// take ...
//put values (5.016605 sec.)
// take OK! : value = XXX (5.017193 sec.)
// take ...
// take OK! : value = YYY (5.017474 sec.)
// take ...
// take OK! : value = ZZZ (5.017649 sec.)
// take ...
//future.cancel
// InterruptedException!
Object[] toArray ()
final var queue = new ArrayBlockingQueue<String>(10);
System.out.println(queue.offer("a")); // true
System.out.println(queue.offer("b")); // true
System.out.println(queue.offer("c")); // true
System.out.println(queue); // [a, b, c]
final Object[] array = queue.toArray();
System.out.println(Arrays.toString(array)); // [a, b, c]
<T> T[] toArray (T[] a)
final var queue = new ArrayBlockingQueue<String>(10);
System.out.println(queue.offer("a")); // true
System.out.println(queue.offer("b")); // true
System.out.println(queue.offer("c")); // true
System.out.println(queue); // [a, b, c]
final String[] array = queue.toArray(new String[0]);
System.out.println(Arrays.toString(array)); // [a, b, c]
final var queue = new ArrayBlockingQueue<String>(10);
System.out.println(queue.offer("a")); // true
System.out.println(queue.offer("b")); // true
System.out.println(queue.offer("c")); // true
System.out.println(queue); // [a, b, c]
{
final String[] array = new String[3];
System.out.println(Arrays.toString(array)); // [null, null, null]
final var ret = queue.toArray(array);
System.out.println(Arrays.toString(array)); // [a, b, c]
System.out.println(Arrays.toString(ret)); // [a, b, c]
}
{
final String[] array = new String[5];
System.out.println(Arrays.toString(array)); // [null, null, null, null, null]
final var ret = queue.toArray(array);
System.out.println(Arrays.toString(array)); // [a, b, c, null, null]
System.out.println(Arrays.toString(ret)); // [a, b, c, null, null]
}
{
final String[] array = new String[1];
System.out.println(Arrays.toString(array)); // [null]
final var ret = queue.toArray(array);
System.out.println(Arrays.toString(array)); // [null]
System.out.println(Arrays.toString(ret)); // [a, b, c]
}
AbstractQueueで宣言されたメソッド
addAll, element, remove
「Java API 使用例 : AbstractQueue」をご参照ください。
AbstractCollectionで宣言されたメソッド
containsAll, isEmpty, toString
「Java API 使用例 : AbstractCollection」をご参照ください。
Collectionで宣言されたメソッド
addAll, containsAll, equals, hashCode, isEmpty, parallelStream, stream, toArray
「Java API 使用例 : Collection」をご参照ください。
Queueで宣言されたメソッド
element, remove
「Java API 使用例 : Queue」をご参照ください。
関連記事
- API 使用例
- BlockingQueue (ブロッキング・キュー)
- Callable
- CancellationException
- ConcurrentHashMap.KeySetView (並列処理用セット)
- ConcurrentLinkedDeque (並列処理用・両端キュー)
- ConcurrentLinkedQueue (並列処理用キュー)
- ConcurrentMap (並列処理用マップ)
- ConcurrentModificationException (並列処理例外)
- ConcurrentSkipListSet (並列処理用セット)
- Condition (同期)
- CopyOnWriteArrayList (並列処理用リスト)
- CopyOnWriteArraySet (並列処理用セット)
- CountDownLatch (同期)
- CyclicBarrier (同期)
- Exchanger (同期)
- Executor
- ExecutorService
- Executors
- Future
- Future.State
- FutureTask
- InterruptedException (割込み例外)
- Lock (同期)
- Object (オブジェクト)
- Runnable
- Semaphore (セマフォ)
- Thread (スレッド)
- ThreadGroup
- ThreadLocal
- TimeUnit