是否有不同步的 Java Stack 的替代品?

Is there a drop-in replacement for Java Stack that is not synchronized?(是否有不同步的 Java Stack 的替代品?)

本文介绍了是否有不同步的 Java Stack 的替代品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Stack 数据结构的大型代码库(由我编写).这是为了方便起见,我有时将其用作堆栈或向量/列表.

I have a large codebase (written by me) that uses the Stack data structure. This was used for convenience and I am using it as Stack sometimes or Vector/List some other times.

然而,经过性能评估,我们决定不为同步安全支付额外费用.我现在需要用非同步的结构替换这个结构(代码中多次提到).

After a performance review however it was decided that we do not want to pay extra for the synchronization safety. I need now to replace this structure with a non-synchronized one (and it is mentioned a lot of times in the code).

我很高兴地发现 Apache 集合包含一个 ArrayStack 这正是我想要的(与 Java 堆栈相同但非同步).但是,这没有像现代 Java 5 代码那样的泛型(这是我使用的).而且我不会将我的代码转换成 Java 1.4 的样子

I was happy to discover that Apache collections includes an ArrayStack which is exactly what I want (same as Java stack but non-synchronized). However this does NOT have generics as modern Java 5 code (which is what I use). And I am not going to convert my code to look like Java 1.4

那么是否有任何其他符合 Java 5 的替代 Java Stack 或者我需要自己编写?

So is there any other Java 5 compliant drop-in replacement for Java Stack or do I need to write my own?

更新:

我将 LinkedList 与经过调整的pop"/push"方法一起使用.

I used LinkedList with tuned "pop"/"push" methods.

推荐答案

当你说Java 5 compliant"时 - ArrayDeque<T> 直到 Java 6 才出现,但听起来像你所追求的(使用 Deque<T> 接口在适当的地方,当然).您可以在需要时将其用作堆栈,也可以将其用作更合适的队列……基本上只需调用适当的方法即可.

When you say "Java 5 compliant" - ArrayDeque<T> didn't arrive until Java 6, but sounds like what you're after (using the Deque<T> interface where appropriate, of course). You can use it as a stack when you want to, or a queue where that's more appropriate... just call the appropriate methods, basically.

这篇关于是否有不同步的 Java Stack 的替代品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:是否有不同步的 Java Stack 的替代品?

基础教程推荐