There is no Collection that offers ordering, concurrency and bounded capacity at the same time, so you have to pick two of those features and add a hand-written solution for the third.
However checking the size is trivial and equally thread-safe with the PriorityBlockingQueue:
while (myQueue.size() >= THRESHOLD) {
Element e = myQueue.poll();
if (e != null) {
process(e);
}
}
myQueue.add(newElement);
Now the well-educated multithreader will immediately spot the non-thread-safe implementation I have chosen (only the counting, the list is always thread-safe), this is however probably ok. The worst thing that could happen is that the queue is pushed over the limit by at maximum as many threads as are asking minus one. So with 4 threads, the queue could be at limit + 3, while with 100 threads it could be at limit + 99.
Now depending on how many threads you use and how critical it is to stay below the limit, there are 3 things you can do:
- Accept that the limit is sometimes violated.
- If you have only a few threads, set the limit to the actual limit minus number of threads.
- Add a synchronization variant to ensure that only one thread is running the above instruction.