Issue
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?
Solution
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.
Answered By - Andy Turner
Answer Checked By - Katrina (JavaFixing Volunteer)