0

I want to have LIFO effect and I want it to be synchronized. 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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Ana Maria
  • 475
  • 2
  • 11
  • Are you talking about `Stack` that is a child of `Vector`? – PM 77-1 Aug 21 '20 at 00:27
  • `Stack` is synchronized, `ArrayDeque` is not. If you want the latter in synchronized, use `LinkedBlockingDeque` or `ConcurrentLinkedDeque`. – akuzminykh Aug 21 '20 at 00:32
  • @PM77-1Yes I am :) – Ana Maria Aug 21 '20 at 00:33
  • @akuzminykh Question is why favor one over another, differences etc.. I know the basic stuff, I don't understand the idea behind it all. – Ana Maria Aug 21 '20 at 00:34
  • 2
    [Why should I use Deque over Stack?](https://stackoverflow.com/questions/12524826/why-should-i-use-deque-over-stack) – akuzminykh Aug 21 '20 at 00:36
  • @akuzminykh I already checked it, not so helpful.. – Ana Maria Aug 21 '20 at 00:37
  • 2
    [Why is Java Vector (and Stack) class considered obsolete or deprecated?](https://stackoverflow.com/questions/1386275/why-is-java-vector-and-stack-class-considered-obsolete-or-deprecated) – akuzminykh Aug 21 '20 at 00:40
  • 1
    Does this answer your question? [Why should I use Deque over Stack?](https://stackoverflow.com/questions/12524826/why-should-i-use-deque-over-stack) – tgdavies Aug 21 '20 at 06:43

1 Answers1

2

From question:

why is it said to favor arrayDequeue?

It is not said to favor ArrayDeque (a class).

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.

The "it is said ..." is right there in the javadoc of Stack:

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.

The Java Runtime Library comes with the following choices of implementation for a Deque:

LinkedBlockingDeque uses locks, which is similar to using synthronized, but none of the others use synchronized. The way ConcurrentLinkedDeque is implemented to be thread-safe has proven to perform better than an implementation using synchronized. ArrayDeque is faster than Stack because it is not using synchronized, so is better for non-thread-safe code.


See also: Why should I use Deque over Stack?
See also: Why is Java Vector (and Stack) class considered obsolete or deprecated?

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Thanks a lot! But forget about all of this, for a second. Decorator for ArrayDeque is absolutely same as Stack, is it? Meaning on performance looking. They both have synchronized each operation, did I get it? – Ana Maria Aug 21 '20 at 11:15
  • @AnaMaria What decorator? There are no built-in decorators for `Deque`. – Andreas Aug 24 '20 at 01:44
  • I could had swore there is collections.synchronizedQueue, because there is for Set,Map,List.. Why not queue as well? :/ – Ana Maria Aug 24 '20 at 11:26