我害怕使用事件总线 http://code.google.com/p/guava-libraries/wiki/EventBusExplained因为我认为它在内部使用无界队列。如果有很多消息发布到它。它可能会遇到完整的 gc。
是否存在一个类似的实现,它做同样的事情是无界队列?
最佳答案
事实上,Guava 使用了一个 ConcurrentLinkedQueue
,这是无界的:
An unbounded thread-safe queue based on linked nodes.
参见 line 151-158 of EventBus.java .
/** queues of events for the current thread to dispatch */ private final ThreadLocal<ConcurrentLinkedQueue<EventWithHandler>> eventsToDispatch = new ThreadLocal<ConcurrentLinkedQueue<EventWithHandler>>() { @Override protected ConcurrentLinkedQueue<EventWithHandler> initialValue() { return new ConcurrentLinkedQueue<EventWithHandler>(); } };
您总是可以修改代码以使用例如ArrayBlockingQueue
.您是否研究过其他类似的解决方案,例如disruptor ?
关于java - 事件总线 Guava 有一个无界队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12362123/