简体   繁体   English

ArrayDeque 的 Stack 和同步装饰器之间的区别

[英]Difference between Stack and synchronization decorator for ArrayDeque

I want to have LIFO effect and I want it to be synchronized.我想有 LIFO 效果,我希望它是同步的。 Does anyone know which one of these two implementations I should use?有谁知道我应该使用这两种实现中的哪一种? Been googling for a while, still no good answer.谷歌搜索了一段时间,仍然没有好的答案。

Bottom line: what are differences, why use one over another, why is it said to favor arrayDequeue?底线:有什么区别,为什么使用一个而不是另一个,为什么说它偏爱arrayDequeue?

From question:从问题:

why is it said to favor arrayDequeue?为什么说有利于arrayDequeue?

It is not said to favor ArrayDeque (a class).据说支持ArrayDeque (一个类)。

It is said that you should favor Deque (an interface) over Stack (a class), because you should program to an interface , allowing you to substitute the implementation without otherwise changing your code.据说你应该更喜欢Deque (一个接口)而不是Stack (一个类),因为你应该编程到一个 interface ,允许你在不改变代码的情况下替换实现

The "it is said..." is right there in the javadoc of Stack : “据说……”就在Stack的 javadoc 中:

A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class. Deque接口及其实现提供了一组更完整一致的 LIFO 堆栈操作,应优先使用此 class。

The Java Runtime Library comes with the following choices of implementation for a Deque : Java 运行时库带有以下Deque实现选择:

LinkedBlockingDeque uses locks, which is similar to using synthronized , but none of the others use synchronized . LinkedBlockingDeque使用锁,这类似于使用synthronized ,但其他都没有使用synchronized The way ConcurrentLinkedDeque is implemented to be thread-safe has proven to perform better than an implementation using synchronized . ConcurrentLinkedDeque被实现为线程安全的方式已被证明比使用synchronized的实现执行得更好。 ArrayDeque is faster than Stack because it is not using synchronized , so is better for non-thread-safe code. ArrayDequeStack快,因为它不使用synchronized ,因此更适合非线程安全代码。


See also: Why should I use Deque over Stack?另请参阅:为什么我应该使用 Deque 而不是 Stack?
See also: Why is Java Vector (and Stack) class considered obsolete or deprecated?另请参阅:为什么 Java 向量(和堆栈)class 被认为已过时或已弃用?

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

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