Class ManyToOneConcurrentLinkedQueue<E>

  • Type Parameters:
    E - element type in the queue.
    All Implemented Interfaces:
    java.lang.Iterable<E>, java.util.Collection<E>, java.util.Queue<E>

    public class ManyToOneConcurrentLinkedQueue<E>
    extends ManyToOneConcurrentLinkedQueueHead<E>
    implements java.util.Queue<E>
    Concurrent linked Queue that can be used from many producers and a single consumer.

    This is a Java port of Dmitry Vyukov's MPSC linked queue.

    Note: This queue breaks the contract for peek and poll in that it can return null when the queue has no item available but size could be greater than zero if an offer is in progress. This is due to the offer being a multiple step process which can start and be interrupted before completion, the thread will later be resumed and the offer process completes. Other methods, such as peek and poll, could spin internally waiting on the offer to complete to provide sequentially consistency across methods but this can have a detrimental effect in a resource starved system. This internal spinning eats up a CPU core and prevents other threads making progress resulting in latency spikes. To avoid this a more relaxed approach is taken in that an in-progress offer is not waited on to complete.

    If you wish to check for empty then call isEmpty() rather than size() checking for zero.

    • Field Detail

      • p128

        byte p128
      • p129

        byte p129
      • p130

        byte p130
      • p131

        byte p131
      • p132

        byte p132
      • p133

        byte p133
      • p134

        byte p134
      • p135

        byte p135
      • p136

        byte p136
      • p137

        byte p137
      • p138

        byte p138
      • p139

        byte p139
      • p140

        byte p140
      • p142

        byte p142
      • p143

        byte p143
      • p144

        byte p144
      • p145

        byte p145
      • p146

        byte p146
      • p147

        byte p147
      • p148

        byte p148
      • p149

        byte p149
      • p150

        byte p150
      • p151

        byte p151
      • p152

        byte p152
      • p153

        byte p153
      • p154

        byte p154
      • p155

        byte p155
      • p156

        byte p156
      • p157

        byte p157
      • p158

        byte p158
      • p159

        byte p159
      • p160

        byte p160
      • p161

        byte p161
      • p162

        byte p162
      • p163

        byte p163
      • p164

        byte p164
      • p165

        byte p165
      • p166

        byte p166
      • p167

        byte p167
      • p168

        byte p168
      • p169

        byte p169
      • p170

        byte p170
      • p171

        byte p171
      • p172

        byte p172
      • p173

        byte p173
      • p174

        byte p174
      • p175

        byte p175
      • p176

        byte p176
      • p177

        byte p177
      • p178

        byte p178
      • p179

        byte p179
      • p180

        byte p180
      • p181

        byte p181
      • p182

        byte p182
      • p183

        byte p183
      • p184

        byte p184
      • p185

        byte p185
      • p186

        byte p186
      • p187

        byte p187
      • p189

        byte p189
      • p190

        byte p190
      • p191

        byte p191
      • p192

        byte p192
      • p193

        byte p193
    • Constructor Detail

      • ManyToOneConcurrentLinkedQueue

        public ManyToOneConcurrentLinkedQueue()
        Constructs an empty queue.
    • Method Detail

      • add

        public boolean add​(E e)
        Specified by:
        add in interface java.util.Collection<E>
        Specified by:
        add in interface java.util.Queue<E>
      • offer

        public boolean offer​(E e)
        Specified by:
        offer in interface java.util.Queue<E>
      • remove

        public E remove()
        Specified by:
        remove in interface java.util.Queue<E>
      • poll

        public E poll()
        Specified by:
        poll in interface java.util.Queue<E>
      • element

        public E element()
        Specified by:
        element in interface java.util.Queue<E>
      • peek

        public E peek()
        Specified by:
        peek in interface java.util.Queue<E>
      • size

        public int size()
        Size can be considered an approximation on a moving list. It is only really stable when the consumer is inactive. If you want to check for queue.size() == 0 then isEmpty() is a better alternative.

        This operation is O(n) on the length of the linked chain.

        Specified by:
        size in interface java.util.Collection<E>
        Returns:
        an approximation for the size of the list.
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<E>
      • contains

        public boolean contains​(java.lang.Object o)
        Specified by:
        contains in interface java.util.Collection<E>
      • iterator

        public java.util.Iterator<E> iterator()
        Specified by:
        iterator in interface java.util.Collection<E>
        Specified by:
        iterator in interface java.lang.Iterable<E>
      • toArray

        public java.lang.Object[] toArray()
        Specified by:
        toArray in interface java.util.Collection<E>
      • toArray

        public <T> T[] toArray​(T[] a)
        Specified by:
        toArray in interface java.util.Collection<E>
      • remove

        public boolean remove​(java.lang.Object o)
        Specified by:
        remove in interface java.util.Collection<E>
      • containsAll

        public boolean containsAll​(java.util.Collection<?> c)
        Specified by:
        containsAll in interface java.util.Collection<E>
      • addAll

        public boolean addAll​(java.util.Collection<? extends E> c)
        Specified by:
        addAll in interface java.util.Collection<E>
      • removeAll

        public boolean removeAll​(java.util.Collection<?> c)
        Specified by:
        removeAll in interface java.util.Collection<E>
      • retainAll

        public boolean retainAll​(java.util.Collection<?> c)
        Specified by:
        retainAll in interface java.util.Collection<E>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<E>
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object