Thread-safe circular buffer in Java

后端 未结 8 1761
不知归路
不知归路 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条回答
  •  猫巷女王i
    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.

提交回复
热议问题