Java : BlockingDeque with Examples

BlockingDeque (Java SE 20 & JDK 20) API Examples.
You will find code examples on most BlockingDeque methods.


Summary

A Deque that additionally supports blocking operations that wait for the deque to become non-empty when retrieving an element, and wait for space to become available in the deque when storing an element.

Class diagram

final long current = System.nanoTime();
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;

try (final var executor = Executors.newSingleThreadExecutor()) {

    final BlockingDeque<String> deque = new LinkedBlockingDeque<>();

    final var future = executor.submit(() -> {
        try {
            while (true) {
                System.out.println("  take ...");
                final var value = deque.takeFirst();

                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());

    deque.putLast("aaa");
    deque.putLast("bbb");
    deque.putLast("ccc");

    TimeUnit.SECONDS.sleep(5);

    System.out.printf("put values (%f sec.)%n", elapsedTime.getAsDouble());

    deque.putLast("XXX");
    deque.putLast("YYY");
    deque.putLast("ZZZ");

    TimeUnit.SECONDS.sleep(5);

    System.out.println("future.cancel");
    future.cancel(true);
}

// Result
// ↓
//put values (0.002596 sec.)
//  take ...
//  take OK! : value = aaa (0.004495 sec.)
//  take ...
//  take OK! : value = bbb (0.004650 sec.)
//  take ...
//  take OK! : value = ccc (0.004762 sec.)
//  take ...
//put values (5.012578 sec.)
//  take OK! : value = XXX (5.012909 sec.)
//  take ...
//  take OK! : value = YYY (5.013045 sec.)
//  take ...
//  take OK! : value = ZZZ (5.013141 sec.)
//  take ...
//future.cancel
//  InterruptedException!

Methods

boolean add (E e)

Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.

This method is equivalent to addLast(E e).

void addFirst (E e)

Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.

// capacity = 3
final BlockingDeque<String> deque = new LinkedBlockingDeque<>(3);
System.out.println(deque); // []

deque.addFirst("aaa");
System.out.println(deque); // [aaa]

deque.addFirst("bbb");
System.out.println(deque); // [bbb, aaa]

deque.addFirst("ccc");
System.out.println(deque); // [ccc, bbb, aaa]

try {
    deque.addFirst("ddd");
} catch (IllegalStateException e) {
    System.out.println("IllegalStateException! : " + e.getMessage());
}

// Result
// ↓
//IllegalStateException! : Deque full

void addLast (E e)

Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.

// capacity = 3
final BlockingDeque<String> deque = new LinkedBlockingDeque<>(3);
System.out.println(deque); // []

deque.addLast("aaa");
System.out.println(deque); // [aaa]

deque.addLast("bbb");
System.out.println(deque); // [aaa, bbb]

deque.addLast("ccc");
System.out.println(deque); // [aaa, bbb, ccc]

try {
    deque.addLast("ddd");
} catch (IllegalStateException e) {
    System.out.println("IllegalStateException! : " + e.getMessage());
}

// Result
// ↓
//IllegalStateException! : Deque full

boolean contains (Object o)

Returns true if this deque contains the specified element.

final BlockingDeque<String> deque = new LinkedBlockingDeque<>();

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque.offerLast("ccc")); // true

System.out.println(deque); // [aaa, bbb, ccc]

System.out.println(deque.contains("aaa")); // true
System.out.println(deque.contains("bbb")); // true
System.out.println(deque.contains("XXX")); // false

E element ()

Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque).

This method is equivalent to getFirst().

final BlockingDeque<String> deque = new LinkedBlockingDeque<>();
System.out.println(deque); // []

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque.offerLast("ccc")); // true

System.out.println(deque.getFirst()); // aaa
System.out.println(deque); // [aaa, bbb, ccc]

System.out.println(deque.removeFirst()); // aaa
System.out.println(deque); // [bbb, ccc]

System.out.println(deque.getFirst()); // bbb
System.out.println(deque); // [bbb, ccc]

System.out.println(deque.removeFirst()); // bbb
System.out.println(deque); // [ccc]

System.out.println(deque.getFirst()); // ccc
System.out.println(deque); // [ccc]

System.out.println(deque.removeFirst()); // ccc
System.out.println(deque); // []

try {
    deque.getFirst();
} catch (NoSuchElementException e) {
    System.out.println("NoSuchElementException!");
}

// Result
// ↓
//NoSuchElementException!

Iterator<E> iterator ()

Returns an iterator over the elements in this deque in proper sequence.

final BlockingDeque<String> deque = new LinkedBlockingDeque<>();

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque.offerLast("ccc")); // true

System.out.println(deque); // [aaa, bbb, ccc]

final var iterator = deque.iterator();

System.out.println("-- forEachRemaining --");
iterator.forEachRemaining(System.out::println);

// Result
// ↓
//-- forEachRemaining --
//aaa
//bbb
//ccc

boolean offer (E e)

Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.

This method is equivalent to offerLast(E e).

boolean offer (E e, long timeout, TimeUnit unit)

Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque), waiting up to the specified wait time if necessary for space to become available.

This method is equivalent to offerLast(E e, long timeout, TimeUnit unit).

boolean offerFirst (E e)

Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.

// capacity = 3
final BlockingDeque<String> deque = new LinkedBlockingDeque<>(3);
System.out.println(deque); // []

System.out.println(deque.offerFirst("aaa")); // true
System.out.println(deque); // [aaa]

System.out.println(deque.offerFirst("bbb")); // true
System.out.println(deque); // [bbb, aaa]

System.out.println(deque.offerFirst("ccc")); // true
System.out.println(deque); // [ccc, bbb, aaa]

System.out.println(deque.offerFirst("ddd")); // false
System.out.println(deque); // [ccc, bbb, aaa]

boolean offerFirst (E e, long timeout, TimeUnit unit)

Inserts the specified element at the front of this deque, waiting up to the specified wait time if necessary for space to become available.

Please see also : offerFirst(E e)

final long current = System.nanoTime();
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;

try (final var executor = Executors.newSingleThreadExecutor()) {

    final BlockingDeque<String> deque = new LinkedBlockingDeque<>(3);
    System.out.printf("remainingCapacity = %d (%f sec.)%n",
            deque.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 = deque.offerFirst(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("deque = %s (%f sec.)%n", deque, elapsedTime.getAsDouble());
}

// Result
// ↓
//remainingCapacity = 3 (0.001915 sec.)
//  offer ...
//  offer ret = true (0.004569 sec.)
//  offer ...
//  offer ret = true (0.004718 sec.)
//  offer ...
//  offer ret = true (0.004860 sec.)
//  offer ...
//  offer ret = false (5.006397 sec.)
//deque = [ccc, bbb, aaa] (10.004461 sec.)

boolean offerLast (E e)

Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.

// capacity = 3
final BlockingDeque<String> deque = new LinkedBlockingDeque<>(3);
System.out.println(deque); // []

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque); // [aaa]

System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque); // [aaa, bbb]

System.out.println(deque.offerLast("ccc")); // true
System.out.println(deque); // [aaa, bbb, ccc]

System.out.println(deque.offerLast("ddd")); // false
System.out.println(deque); // [aaa, bbb, ccc]

boolean offerLast (E e, long timeout, TimeUnit unit)

Inserts the specified element at the end of this deque, waiting up to the specified wait time if necessary for space to become available.

Please see also : offerLast(E e)

final long current = System.nanoTime();
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;

try (final var executor = Executors.newSingleThreadExecutor()) {

    final BlockingDeque<String> deque = new LinkedBlockingDeque<>(3);
    System.out.printf("remainingCapacity = %d (%f sec.)%n",
            deque.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 = deque.offerLast(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("deque = %s (%f sec.)%n", deque, elapsedTime.getAsDouble());
}

// Result
// ↓
//remainingCapacity = 3 (0.001915 sec.)
//  offer ...
//  offer ret = true (0.004569 sec.)
//  offer ...
//  offer ret = true (0.004718 sec.)
//  offer ...
//  offer ret = true (0.004860 sec.)
//  offer ...
//  offer ret = false (5.006397 sec.)
//deque = [aaa, bbb, ccc] (10.007953 sec.)

E peek ()

Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.

This method is equivalent to peekFirst().

final BlockingDeque<String> deque = new LinkedBlockingDeque<>();
System.out.println(deque); // []

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque.offerLast("ccc")); // true

System.out.println(deque.peekFirst()); // aaa
System.out.println(deque); // [aaa, bbb, ccc]

System.out.println(deque.pollFirst()); // aaa
System.out.println(deque); // [bbb, ccc]

System.out.println(deque.peekFirst()); // bbb
System.out.println(deque); // [bbb, ccc]

System.out.println(deque.pollFirst()); // bbb
System.out.println(deque); // [ccc]

System.out.println(deque.peekFirst()); // ccc
System.out.println(deque); // [ccc]

System.out.println(deque.pollFirst()); // ccc
System.out.println(deque); // []

System.out.println(deque.peekFirst()); // null
System.out.println(deque); // []

E poll ()

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.

This method is equivalent to pollFirst().

final BlockingDeque<String> deque = new LinkedBlockingDeque<>();
System.out.println(deque); // []

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque.offerLast("ccc")); // true

System.out.println(deque); // [aaa, bbb, ccc]

System.out.println(deque.pollFirst()); // aaa
System.out.println(deque); // [bbb, ccc]

System.out.println(deque.pollFirst()); // bbb
System.out.println(deque); // [ccc]

System.out.println(deque.pollFirst()); // ccc
System.out.println(deque); // []

System.out.println(deque.pollFirst()); // null
System.out.println(deque); // []

E poll (long timeout, TimeUnit unit)

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), waiting up to the specified wait time if necessary for an element to become available.

This method is equivalent to pollFirst(long timeout, TimeUnit unit).

E pollFirst (long timeout, TimeUnit unit)

Retrieves and removes the first element of this deque, waiting up to the specified wait time if necessary for an element to become available.

Please see also : poll()

final long current = System.nanoTime();
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;

try (final var executor = Executors.newSingleThreadExecutor()) {

    final BlockingDeque<String> deque = new LinkedBlockingDeque<>();

    System.out.printf("put values (%f sec.)%n", elapsedTime.getAsDouble());

    deque.putLast("aaa");
    deque.putLast("bbb");
    deque.putLast("ccc");

    final var future = executor.submit(() -> {
        try {
            while (true) {
                System.out.println("  poll ...");
                final var value = deque.pollFirst(5, TimeUnit.SECONDS);

                System.out.printf("  poll value = %s (%f sec.)%n",
                        value, elapsedTime.getAsDouble());
            }
        } catch (InterruptedException e) {
            System.out.println("  InterruptedException!");
        }
    });

    TimeUnit.SECONDS.sleep(8);

    System.out.println("future.cancel");
    future.cancel(true);
}

// Result
// ↓
//put values (0.002583 sec.)
//  poll ...
//  poll value = aaa (0.003982 sec.)
//  poll ...
//  poll value = bbb (0.004155 sec.)
//  poll ...
//  poll value = ccc (0.004282 sec.)
//  poll ...
//  poll value = null (5.007742 sec.)
//  poll ...
//future.cancel
//  InterruptedException!

E pollLast (long timeout, TimeUnit unit)

Retrieves and removes the last element of this deque, waiting up to the specified wait time if necessary for an element to become available.

final long current = System.nanoTime();
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;

try (final var executor = Executors.newSingleThreadExecutor()) {

    final BlockingDeque<String> deque = new LinkedBlockingDeque<>();

    System.out.printf("put values (%f sec.)%n", elapsedTime.getAsDouble());

    deque.putLast("aaa");
    deque.putLast("bbb");
    deque.putLast("ccc");

    final var future = executor.submit(() -> {
        try {
            while (true) {
                System.out.println("  poll ...");
                final var value = deque.pollLast(5, TimeUnit.SECONDS);

                System.out.printf("  poll value = %s (%f sec.)%n",
                        value, elapsedTime.getAsDouble());
            }
        } catch (InterruptedException e) {
            System.out.println("  InterruptedException!");
        }
    });

    TimeUnit.SECONDS.sleep(8);

    System.out.println("future.cancel");
    future.cancel(true);
}

// Result
// ↓
//put values (0.002719 sec.)
//  poll ...
//  poll value = ccc (0.004141 sec.)
//  poll ...
//  poll value = bbb (0.004277 sec.)
//  poll ...
//  poll value = aaa (0.004409 sec.)
//  poll ...
//  poll value = null (5.006775 sec.)
//  poll ...
//future.cancel
//  InterruptedException!

void push (E e)

Pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.

This method is equivalent to addFirst(E e).

void put (E e)

Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque), waiting if necessary for space to become available.

This method is equivalent to putLast(E e).

void putFirst (E e)

Inserts the specified element at the front of this deque, waiting if necessary for space to become available.

final long current = System.nanoTime();
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;

try (final var executor = Executors.newSingleThreadExecutor()) {

    final BlockingDeque<String> deque = new LinkedBlockingDeque<>(3);
    System.out.printf("remainingCapacity = %d (%f sec.)%n",
            deque.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 ...");
                deque.putFirst(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("deque = %s (%f sec.)%n", deque, elapsedTime.getAsDouble());

    System.out.println("future.cancel");
    future.cancel(true);
}

// Result
// ↓
//remainingCapacity = 3 (0.002041 sec.)
//  put ...
//  put OK! (0.004570 sec.)
//  put ...
//  put OK! (0.004671 sec.)
//  put ...
//  put OK! (0.004771 sec.)
//  put ...
//deque = [ccc, bbb, aaa] (5.018881 sec.)
//future.cancel
//  InterruptedException! (5.019745 sec.)

void putLast (E e)

Inserts the specified element at the end of this deque, waiting if necessary for space to become available.

final long current = System.nanoTime();
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;

try (final var executor = Executors.newSingleThreadExecutor()) {

    final BlockingDeque<String> deque = new LinkedBlockingDeque<>(3);
    System.out.printf("remainingCapacity = %d (%f sec.)%n",
            deque.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 ...");
                deque.putLast(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("deque = %s (%f sec.)%n", deque, elapsedTime.getAsDouble());

    System.out.println("future.cancel");
    future.cancel(true);
}

// Result
// ↓
//remainingCapacity = 3 (0.001776 sec.)
//  put ...
//  put OK! (0.004289 sec.)
//  put ...
//  put OK! (0.004391 sec.)
//  put ...
//  put OK! (0.004498 sec.)
//  put ...
//deque = [aaa, bbb, ccc] (5.007701 sec.)
//future.cancel
//  InterruptedException! (5.008613 sec.)

E remove ()

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque).

This method is equivalent to removeFirst().

final BlockingDeque<String> deque = new LinkedBlockingDeque<>();
System.out.println(deque); // []

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("bbb")); // true
System.out.println(deque.offerLast("ccc")); // true

System.out.println(deque); // [aaa, bbb, ccc]

System.out.println(deque.removeFirst()); // aaa
System.out.println(deque); // [bbb, ccc]

System.out.println(deque.removeFirst()); // bbb
System.out.println(deque); // [ccc]

System.out.println(deque.removeFirst()); // ccc
System.out.println(deque); // []

try {
    deque.removeFirst();
} catch (NoSuchElementException e) {
    System.out.println("NoSuchElementException!");
}

// Result
// ↓
//NoSuchElementException!

boolean remove (Object o)

Removes the first occurrence of the specified element from this deque.

This method is equivalent to removeFirstOccurrence(Object o).

boolean removeFirstOccurrence (Object o)

Removes the first occurrence of the specified element from this deque.

final BlockingDeque<String> deque = new LinkedBlockingDeque<>();

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("BBB")); // true
System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("BBB")); // true
System.out.println(deque.offerLast("ccc")); // true

System.out.println(deque); // [aaa, BBB, aaa, BBB, ccc]

System.out.println(deque.removeFirstOccurrence("aaa")); // true
System.out.println(deque); // [BBB, aaa, BBB, ccc]

System.out.println(deque.removeFirstOccurrence("BBB")); // true
System.out.println(deque); // [aaa, BBB, ccc]

System.out.println(deque.removeFirstOccurrence("XXX")); // false
System.out.println(deque); // [aaa, BBB, ccc]

boolean removeLastOccurrence (Object o)

Removes the last occurrence of the specified element from this deque.

final BlockingDeque<String> deque = new LinkedBlockingDeque<>();

System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("BBB")); // true
System.out.println(deque.offerLast("aaa")); // true
System.out.println(deque.offerLast("BBB")); // true
System.out.println(deque.offerLast("ccc")); // true

System.out.println(deque); // [aaa, BBB, aaa, BBB, ccc]

System.out.println(deque.removeLastOccurrence("BBB")); // true
System.out.println(deque); // [aaa, BBB, aaa, ccc]

System.out.println(deque.removeLastOccurrence("aaa")); // true
System.out.println(deque); // [aaa, BBB, ccc]

System.out.println(deque.removeLastOccurrence("XXX")); // false
System.out.println(deque); // [aaa, BBB, ccc]

int size ()

Returns the number of elements in this deque.

final BlockingDeque<String> deque = new LinkedBlockingDeque<>();
System.out.println(deque); // []
System.out.println(deque.size()); // 0

System.out.println(deque.offerLast("aaa")); // true

System.out.println(deque); // [aaa]
System.out.println(deque.size()); // 1

System.out.println(deque.offerLast("bbb")); // true

System.out.println(deque); // [aaa, bbb]
System.out.println(deque.size()); // 2

System.out.println(deque.offerLast("ccc")); // true

System.out.println(deque); // [aaa, bbb, ccc]
System.out.println(deque.size()); // 3

E take ()

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), waiting if necessary until an element becomes available.

This method is equivalent to takeFirst().

E takeFirst ()

Retrieves and removes the first element of this deque, waiting if necessary until an element becomes available.

final long current = System.nanoTime();
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;

try (final var executor = Executors.newSingleThreadExecutor()) {

    final BlockingDeque<String> deque = new LinkedBlockingDeque<>();

    final var future = executor.submit(() -> {
        try {
            while (true) {
                System.out.println("  take ...");
                final var value = deque.takeFirst();

                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());

    deque.putLast("aaa");
    deque.putLast("bbb");
    deque.putLast("ccc");

    TimeUnit.SECONDS.sleep(5);

    System.out.printf("put values (%f sec.)%n", elapsedTime.getAsDouble());

    deque.putLast("XXX");
    deque.putLast("YYY");
    deque.putLast("ZZZ");

    TimeUnit.SECONDS.sleep(5);

    System.out.println("future.cancel");
    future.cancel(true);
}

// Result
// ↓
//put values (0.002596 sec.)
//  take ...
//  take OK! : value = aaa (0.004495 sec.)
//  take ...
//  take OK! : value = bbb (0.004650 sec.)
//  take ...
//  take OK! : value = ccc (0.004762 sec.)
//  take ...
//put values (5.012578 sec.)
//  take OK! : value = XXX (5.012909 sec.)
//  take ...
//  take OK! : value = YYY (5.013045 sec.)
//  take ...
//  take OK! : value = ZZZ (5.013141 sec.)
//  take ...
//future.cancel
//  InterruptedException!

E takeLast ()

Retrieves and removes the last element of this deque, waiting if necessary until an element becomes available.

final long current = System.nanoTime();
final DoubleSupplier elapsedTime = () -> (System.nanoTime() - current) / 1000000000.0;

try (final var executor = Executors.newSingleThreadExecutor()) {

    final BlockingDeque<String> deque = new LinkedBlockingDeque<>();

    System.out.printf("put values (%f sec.)%n", elapsedTime.getAsDouble());

    deque.putLast("aaa");
    deque.putLast("bbb");
    deque.putLast("ccc");

    final var future = executor.submit(() -> {
        try {
            while (true) {
                System.out.println("  take ...");
                final var value = deque.takeLast();

                System.out.printf("  take OK! : value = %s (%f sec.)%n",
                        value, elapsedTime.getAsDouble());
            }
        } catch (InterruptedException e) {
            System.out.println("  InterruptedException!");
        }
    });

    TimeUnit.SECONDS.sleep(5);

    System.out.printf("future.cancel (%f sec.)%n", elapsedTime.getAsDouble());
    future.cancel(true);
}

// Result
// ↓
//put values (0.002098 sec.)
//  take ...
//  take OK! : value = ccc (0.004629 sec.)
//  take ...
//  take OK! : value = bbb (0.004766 sec.)
//  take ...
//  take OK! : value = aaa (0.004897 sec.)
//  take ...
//future.cancel (5.011561 sec.)
//  InterruptedException!

Methods declared in BlockingQueue

drainTo, drainTo, remainingCapacity

Please see the link below.

Methods declared in Collection

clear, containsAll, equals, hashCode, isEmpty, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray

Please see the link below.

Methods declared in Deque

addAll, descendingIterator, getFirst, getLast, peekFirst, peekLast, pollFirst, pollLast, pop, removeFirst, removeLast

Please see the link below.

Methods declared in Iterable

forEach

Please see the link below.


Related posts

To top of page