Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am new to Java and trying to use:

public static ArrayBlockingQueue<String> qu=new ArrayBlockingQueue<>(900);
...
qu.enqueue(MyString);

or

public static Queue<String> qu=new Queue<>(900);
...
qu.enqueue(MyString);

Queue screams,claiming it's only an interface. ArrayBlockingQueue does not accept enqueue. I feel I'm making some basic mistake. My question is: do I have to write my own Queue implementing the interface every time I use it or is there a ready function? If yes, what am I doing wrong? My second question is: what is a standard queue size? From the specifications I've read I assumed it must be the maximum I may use but isn't 900 a bit big space-taker for a small application?

share|improve this question
1  
note that you only need to use the blocking queue if you know you are modifying the queue in more than one thread – David Hofmann 18 hours ago
David is right. For single-threaded access, ArrayDeque is a good choice. – erickson 17 hours ago
Thanks, I'll keep that in mind, but I have a ThreadPool working on this data, so I guess not this time. – arletka 17 hours ago

3 Answers

up vote 1 down vote accepted

Just use add and offer instead of enqueue.

An array of size 900 is tiny and not worth worrying about. Use as many elements as you want to allow the queue to hold.

share|improve this answer

You are trying to create an instance of an interface, while you should instantiate a class implementing it. Like this:

public Queue<Integer> intQueue = new ArrayBlockingQueue<Integer>(10);

share|improve this answer

Here's what I'd recommend as a Queue member in a single-threaded application:

private final Queue<String> someLogicalName = new ArrayDeque<>();

Let's break this down.

The private modifier means that only the class (or instances of the class) in which this declaration appears can monkey with your queue.

The final modifier means that the member can't be reassigned. If you never plan to change the value, make that assumption explicit with the final modifier, and the compiler will tell you when you violate your assumption accidentally. And, if multiple threads will access the variable, making it final is one of the ways to obtain a guarantee that all threads will see its assigned value, rather than null.

I declare the variable as Queue, since that's the interface I want the code to work against. I don't declare it as the implementation class; this could prevent me from changing the implementation type later as my requirements change.

The name qu doesn't tell us anything. What if you have a few queues? Would you name them "qu", "qu2", "qu3"? That could quickly get confusing. Pick logical names that describe the role of the information in the variable, not its type.

How do I find an implementation class for Queue? Look at the documentation. See the section with the heading "All Known Implementing Classes?" Most of those are concrete types, and they fulfill the Queue interface. You can read the documentation for each of them to see what the differences are, and select the one appropriate for your use. While Queue has more implementations than most collection types, they follow naming patterns that make it easy to narrow down what you need. I picked a simple, single-threaded queue, but if you are working with a thread pool, you'll probably want a queue from the java.util.concurrent package.

The compiler can infer the type parameters for the constructor, so you can use the "diamond operator," <> to reduce clutter.

share|improve this answer
And, if multiple threads will access the variable, making it final is one of the ways to obtain a guarantee that all threads will see its assigned value, rather than null. Wouldn't it be better to use "volatile" in that context? – arletka 17 hours ago
@arletka It's not better. It is necessary if the value of the variable must change. But, if not, it's better to make it final, since it makes your program easier to understand (and it might also be faster to read the value). – erickson 15 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.