![]() ![]() Stack also has an insertElementAt() method that we can use to insert elements in the middle of the stack. Since Stack and Deque ultimately implement the Collection interface, they have methods such as remove(), removeIf(), removeAll(), and ratainAll() that can be used to remove elements from the middle of the data structure. Thus, both violate the interface segregation principle. Violation of the Interface Segregation Principleīoth the Stack class and the Deque interface define methods that the respective data structure should not offer. ¹ The Java Deque interface inherits from Queue, therefore, ArrayDeque can be used as both a deque and a queue. thread-safe implementations with optimistic locking (e.g., ConcurrentLinkedDeque).thread-safe implementations with pessimistic locking (e.g., LinkedBlockingDeque).The JDK also offers numerous implementations for the Deque interface: 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 For 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. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |