In fact, the JDK developers recommend not to use the Stack class and instead use implementations of the Deque interface, which also defines the stack methods push() and pop(). thread-safe implementations with optimistic locking (e.g., ConcurrentLinkedQueue).thread-safe implementations with pessimistic locking (e.g., LinkedBlockingQueue).non-thread-safe implementations (e.g., ArrayDeque¹).For moderate access conflicts, optimistic locking would be more appropriate.įor the Queue interface, the JDK offers several implementations: However, if we do not need thread safety, synchronization is unnecessary.Īnd if we need thread safety, the use of pessimistic locking, as synchronized uses it, would only make sense for a high number of access conflicts ("high thread contention"). Thread SafetyĪll Stack methods are synchronized – Stack is, therefore, thread-safe. You can choose a suitable queue implementation according to your application area. The JDK provides several queue implementations with different characteristics. Queue, on the other hand, is an interface (→ all details about the Queue interface). Stack is a class (→ all details about the Stack class), i.e., a concrete implementation of the stack data type in the JDK. This section describes the differences between the Java class and the interface concerning various aspects. The "bottom" of the stack is not accessible via the operations. The insertion and removal operation as well as the sides of the data structures are named differently for stacks and queues: Operation Queue data structureįor more details, such as areas of application and considerations of time complexity, see the main article on the stack data structure and the main article on the queue data structure. The first elements to be inserted in the queue are also the first to be removed, and the elements inserted last are removed last. Stack data structureĪ queue is a linear data structure in which the elements are inserted and removed according to the FIFO principle ("first-in-first-out"). That means that the element placed on the stack last is the first to be removed – and the element placed on the stack first is removed last. Difference between Stack and QueueĪ stack is a linear data structure where the elements are inserted and removed according to the LIFO principle ("last-in-first-out").
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |