0

Currently I'm reading Collections in java. As I read, LinkedTransferQueue is superset of LinkedBlockingQueue whose put(E e) method inserts and object and returns void and if need, will block until space in the queue becomes available. But I don't see any constructor of LinkedTransferQueue which accepts capacity to bound it by size.

So how and when an invocation of put method on a LinkedBlockingQueue will block by considering it full as we did not specify a bounded size for it anywhere.

I came across following lines of code which i did not get.

TransferQueue<Integer> tq = new LinkedTransferQueue<>();  // not bounded.
tq.put(2);      // blocks if bounded and full

what is the meaning of comment at line-2.

Thanks.

Vivek Singh
  • 2,047
  • 11
  • 24
Deepankar Singh
  • 662
  • 1
  • 9
  • 20

2 Answers2

3

Even though LinkedTransferQueue cannot be capacity bounded, the interface TransferQueue allows implementations to be bounded.

Like other blocking queues, a TransferQueue may be capacity bounded.

Currently the only implementation is the unbounded LinkedTransferQueue, so it's just a matter of wording. I.e. a TransferQueue is not guaranteed to be unbounded.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • you mean a LinkedtransferQueue is not bonded by size and any number of invocations of put method on it can be done and it wont block as there is no upper bound? – Deepankar Singh Nov 04 '15 at 11:19
  • That's right. But you could write your own `TransferQueue` implementation and make it bounded, which would be okay according to the contract of the interface. – Kayaman Nov 04 '15 at 11:20
  • So comment here at line-2 ("blocks if bounded and full") is wrong/misleading here as it can never be full. I read it in Kathy Sierra book for OCJP-7 certification. – Deepankar Singh Nov 04 '15 at 11:25
  • Yes it's a bit misleading IMO. – Kayaman Nov 04 '15 at 11:26
  • 1
    A bit misleading but it refers to the put method of the interface. By contract it could block, thus the comment, but in that specific instance, because you know the TransferQueue is really a LinkedTransferQueue, it will not block. Semantics, maybe, but still correct, just a bit confusing. – Buurman Nov 04 '15 at 11:39
  • Indeed, not really the wording you'd want in a book such as that. – Kayaman Nov 04 '15 at 11:46
0
/**
 * Creates a {@code LinkedBlockingQueue} with the given (fixed) capacity.
 *
 * @param capacity the capacity of this queue
 * @throws IllegalArgumentException if {@code capacity} is not greater
 *         than zero
 */
public LinkedBlockingQueue(int capacity) {
    if (capacity <= 0) throw new IllegalArgumentException();
    this.capacity = capacity;
    last = head = new Node<E>(null);
}

This is LinkedBlockingQueue constructor code, it receives a param to set capacity.

/**
 * Always returns {@code Integer.MAX_VALUE} because a
 * {@code LinkedTransferQueue} is not capacity constrained.
 *
 * @return {@code Integer.MAX_VALUE} (as specified by
 *         {@link java.util.concurrent.BlockingQueue#remainingCapacity()
 *         BlockingQueue.remainingCapacity})
 */
public int remainingCapacity() {
    return Integer.MAX_VALUE;
}

This is LinkedTransferQueue class code, it's capacity always equal to Integer.MAX_VALUE.

He Yuntao
  • 86
  • 11