Java中的线程安全循环缓冲区 [英] Thread-safe circular buffer in Java
问题描述
考虑一些并行运行的Web服务器实例。每个服务器都拥有对单个共享Status keeper的引用,其角色是保留所有服务器的最后一个 N
请求。
例如( N = 3
):
服务器a:请求ID = ABCD状态管理器= [ABCD]
服务器b:请求ID = XYZZ状态管理器= [ABCD,XYZZ]
服务器c:请求id = 1234Status keeper = [ABCD,XYZZ,1234]
服务器b:请求id = FOO状态管理器= [XYZZ,1234,FOO]
服务器a:请求ID = BAR状态管理员= [1234,FOO,BAR]
在任何时候,都可以从监控应用程序中调用状态管理器,该应用程序读取这些最后的 N
SLA报告请求。 / p>
在Java中实现这种生产者 - 消费者场景的最佳方法是什么,使Web服务器的优先级高于SLA报告?
CircularFifoBuffer 似乎是保存请求的合适数据结构,但我不确定什么是最佳方式实现高效的并发。
Buffer fifo = BufferUtils.synchronizedBuffer(new CircularFifoBuffer()) ;
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
requests from all servers.
For example (N=3
):
Server a: "Request id = ABCD" Status keeper=["ABCD"]
Server b: "Request id = XYZZ" Status keeper=["ABCD", "XYZZ"]
Server c: "Request id = 1234" Status keeper=["ABCD", "XYZZ", "1234"]
Server b: "Request id = FOO" Status keeper=["XYZZ", "1234", "FOO"]
Server a: "Request id = BAR" Status keeper=["1234", "FOO", "BAR"]
At any point in time, the "Status keeper" might be called from a monitoring application that reads these last N
requests for an SLA report.
What's the best way to implement this producer-consumer scenario in Java, giving the web servers higher priority than the SLA report?
CircularFifoBuffer seems to be the appropriate data structure to hold the requests, but I'm not sure what's the optimal way to implement efficient concurrency.
Buffer fifo = BufferUtils.synchronizedBuffer(new CircularFifoBuffer());
这篇关于Java中的线程安全循环缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!