![]() In the next parts of this series, I'll show you how to implement queues yourself, starting with implementing a queue with a stack. This article has provided an overview of all Queue implementations available in Java, as well as a decision aid for which cases to use which queue. The open-source library JCTools provides highly optimized queue implementations for all four cases. With special mechanisms, it is possible to optimize queues so that the overhead for maintaining thread safety is minimized when there is a restriction to one reading and/or one writing thread.Īccordingly, the following four cases are distinguished: This means that one or more writing threads and one or more reading threads can access the JDK queues concurrently. Here is the process in the form of a decision tree:ĭecision tree Java Queue implementations Optimized MPMC, MPSC, SPMC, and SPSC QueuesĪll thread-safe queue implementations provided by the JDK can be used in multi-producer-multi-consumer environments. LinkedBlockingQueue as a thread-safe, blocking, bounded queue if you expect high contention between producer and consumer threads (best to test which implementation is more performant for your use case).ArrayBlockingQueue as a thread-safe, blocking, bounded queue if you expect low to medium contention between producer and consumer threads.ConcurrentLinkedQueue as a thread-safe, non-blocking, and unbounded queue.ArrayDeque for single-threaded applications.Using the characteristics of the queue implementations described in the respective articles and summarized in the table above, you can find the proper queue for each use case.įor day-to-day use of general queue implementations, I make the following recommendations: When Should You Use Which Queue Implementation? ² Fail-fast: The iterator throws a ConcurrentModificationException if elements are added to or removed from the queue during iteration. Changes that occur after this can, but do not need to, be reflected by the iterator. ![]() ¹ Weakly consistent: All elements that exist when the iterator is created are traversed by the iterator exactly once. (optimistic locking through compare-and-set) The class names in the following table are linked to that article of the tutorial series in which the respective Queue implementation is explained in detail.įor an explanation of the terms blocking, non-blocking, fairness policy, bounded, and unbounded, see the article about the BlockingQueue interface. This article provides an overview of all Queue implementations available in the JDK, including their characteristics, as well as a decision support for which implementation is best suited for which purpose. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |