user1624552 February 2016

how to know how many elements are currently in a BlockingQueue using Java and Spring

I have a Java application and I am using Spring Framework.

Using beans I define a queue with its size set to a maximum of 5 elements like below:

  <bean id="Queue1" class="java.util.concurrent.PriorityBlockingQueue">
      <constructor-arg type="int"><value>5</value></constructor-arg>
  </bean>

Then I pass it to the constructor in Java using injection and I initialize it within the constructor. In Java I have defined and done below:

private final BlockingQueue<OrdenTrabajo> queue;
...
this.queue = queue
...

Later, when I try to get queue size in code using:

queue.size()

it returns 0 instead of 5. Why?

Also, is there any possibility to obtain the number of elements that are currently in the queue?

Answers


Andy Turner February 2016

The int constructor parameter is an initial capacity, not the number of elements present initially.

PriorityBlockingQueue.size() returns the number of elements present in the collection; PriorityBlockingQueue.remainingCapacity() returns the available capacity.

Post Status

Asked in February 2016
Viewed 2,747 times
Voted 9
Answered 1 times

Search




Leave an answer