简体   繁体   English

Java队列实现

[英]Java Queue Implementation

I am new to Java and trying to use: 我是Java新手并尝试使用:

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. ArrayBlockingQueue不接受入队。 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? 我的问题是:每次使用它时,我是否必须编写自己的Queue来实现接口,或者是否有就绪函数? 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? 根据我读过的规格,我认为它必须是我可以使用的最大值,但对于小型应用来说,900不是一个很大的空间接受者吗?

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);

Just use add and offer instead of enqueue . 只需使用add and offer而不是enqueue

An array of size 900 is tiny and not worth worrying about. 尺寸为900的阵列很小,不值得担心。 Use as many elements as you want to allow the queue to hold. 使用尽可能多的元素来允许队列保留。

Here's what I'd recommend as a Queue member in a single-threaded application: 以下是我在单线程应用程序中作为Queue成员推荐的内容:

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. private修饰符意味着只有出现此声明的类(或类的实例)才能使用您的队列。

The final modifier means that the member can't be reassigned. final修饰符意味着无法重新分配成员。 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. 如果您从未计划更改该值,请使用final修饰符明确表示该假设,并且编译器会在您意外违反您的假设时告诉您。 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 . 并且,如果多个线程将访问变量,那么使其成为final是获得所有线程将看到其指定值而不是null的保证的方法之一。

I declare the variable as Queue , since that's the interface I want the code to work against. 我将变量声明为Queue ,因为那是我希望代码工作的接口。 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. 名称qu不告诉我们任何事情。 What if you have a few queues? 如果你有几个队列怎么办? Would you name them "qu", "qu2", "qu3"? 你会把它们命名为“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 ? 如何找到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. 其中大多数是具体类型,它们满足Queue接口。 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. 虽然Queue具有比大多数集合类型更多的实现,但它们遵循命名模式,可以轻松缩小您的需求范围。 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. 我选择了一个简单的单线程队列,但如果您正在使用线程池,则可能需要java.util.concurrent包中的队列。

The compiler can infer the type parameters for the constructor, so you can use the "diamond operator," <> to reduce clutter. 编译器可以推断构造函数的类型参数,因此可以使用“菱形运算符” <>来减少混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM