Boom Boom - 1 year ago 86
Java Question

How can I implement a blocking queue with primive types?

Is there a Java native implementation which uses a blocking queue with primitive types? If not, how can I build one?

I want to use blocking queue without boxing and unboxing when using primitives types. I check the trove assets, but it doesn't support blocking queue.

Answer Source

As far as I know there's no built-in blocking queue for primitive types.

This leaves you with two options:

  1. Avoid primitive types. Use Java's wrappers such as Integer, Float etc. This is what I'd recommend unless you have a very good reason for avoiding references (are you that afraid of the extra memory? How many items do you plan to keep in the queue?)

  2. Implement a blocking queue yourself. It should be fairly straightforward using a simple array and Semaphore. Although you can probably get better performance if you copy the OpenJDK's implementation and make the required changes. Just change private final E[] items to something like private final int[] items, then make any additional changes that may be required where items is referenced.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download