3

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.

Malt
  • 28,965
  • 9
  • 65
  • 105

1 Answers1

2

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.

Malt
  • 28,965
  • 9
  • 65
  • 105
  • 1. Do you think that OP is unaware of this option? Moreover, "avoid primitive types" sounds like a perfectly bad general advice for Java. It adds nothing to clarity, supportability, etc, OTOH it opens possibility for bugs with null. The only actual reason to avoid primitive types is the lack of support from libraries, which is exactly the topic of this question. – leventov Sep 12 '16 at 07:35
  • 2. It's not that simple given OpenJDK's source is released under GPL with CP exception, that is hard to follow properly in order to not being have to open your whole proprietary codebase. And anyway it is a no starter for many companies. [Version from DL's website](http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/java/util/concurrent/ArrayBlockingQueue.java?view=log) should be rather used, because it is public domain. – leventov Sep 12 '16 at 07:56
  • @leventov 1. yes. OP could be unaware of this option. Neither of us have no idea who he is, he might be a student, a beginner, or both (like many of the site's visitors). That seems likely judging by the question. Without any additional information, and unless there's a specific need, I'd say that replacing `Integer`s with `Int`s is a classic premature optimization that I've seen people do (especially beginners coming from C/C++) – Malt Sep 12 '16 at 20:22
  • @leventov 2. The link I provided clearly states the license under which the code is provided. OP has an option of modifying and using it (under GPL), or following the second implementation using an array and an a semaphore (or finding some other implementation, like the one you linked directly from Dough Lea). – Malt Sep 12 '16 at 20:33
  • IMO "It's a premature optimization" is not a good answer to any performance related questions on SO. Such statements should better be comments to questions. – leventov Sep 12 '16 at 20:58
  • Source from Doug Lea is not other implementation, it's exactly the same implementation (you can compare it with current [JDK9 head version](http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/02d65bf86352/src/java.base/share/classes/java/util/concurrent/ArrayBlockingQueue.java)), but it has different license. – leventov Sep 12 '16 at 21:02