Thread-safe circular buffer in Java

后端 未结 8 1720
不知归路
不知归路 2020-12-08 05:23

Consider a few web server instances running in parallel. Each server holds a reference to a single shared \"Status keeper\", whose role is keeping the last N re

相关标签:
8条回答
  • 2020-12-08 05:31
    Buffer fifo = BufferUtils.synchronizedBuffer(new CircularFifoBuffer());
    
    0 讨论(0)
  • 2020-12-08 05:32

    I would have a look at ArrayDeque, or for a more concurrent implementation have a look at the Disruptor library which is one of the most sophisticated/complex ring buffer in Java.

    An alternative is to use an unbounded queue which is more concurrent as the producer never needs to wait for the consumer. Java Chronicle

    Unless your needs justify the complexity, an ArrayDeque may be all you need.

    0 讨论(0)
  • 2020-12-08 05:34

    Maybe you want to look at Disruptor - Concurrent Programming Framework.

    • Find a paper describing the alternatives, design and also a performance comparement to java.util.concurrent.ArrayBlockingQueue here: pdf
    • Consider to read the first three articles from BlogsAndArticles

    If the library is too much, stick to java.util.concurrent.ArrayBlockingQueue

    0 讨论(0)
  • 2020-12-08 05:37

    Also have a look at java.util.concurrent.

    Blocking queues will block until there is something to consume or (optionally) space to produce:

    http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/BlockingQueue.html

    Concurrent linked queue is non-blocking and uses a slick algorithm that allows a producer and consumer to be active concurrently:

    http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html

    0 讨论(0)
  • 2020-12-08 05:44

    Hazelcast's Queue offers almost everything you ask for, but doesn't support circularity. But from your description I am not sure if you actually need it.

    0 讨论(0)
  • 2020-12-08 05:44

    Since you specifically ask to give writers (that is web servers) higher priority than the reader (that is monitoring), I would suggest the following design.

    Web servers add request information to a concurrent queue which is read by a dedicated thread, which adds requests to a thread-local (therefore non-synchronized) queue that overwrites the oldest element, like EvictingQueue or CircularFifoQueue. This same thread checks a flag which indicates if a report has been requested after every request processed, and if positive, produces a report from all elements present in the thread-local queue.

    0 讨论(0)
提交回复
热议问题