删除侦听器后,可以再在该侦听器上调用该事件一次吗? [英] When a listener is removed, is it okay that the event be called on that listener one more time?
问题描述
大多数人使用ConcurrentLinkedQueue或CopyOnWriteArrayList来收集侦听器,并在发生某些情况时通知他们.这样做的缺点是,侦听程序可能会在自己未注册的情况下获得通知.如果管理器(集合,...,给它命名)正在遍历侦听器的集合,并且在该迭代过程中某些侦听器取消了自己的注册,则会发生这种情况.
Most people use a ConcurrentLinkedQueue or CopyOnWriteArrayList to collect listeners and notify them when something happens. The drawback of that is that it is possible that a listener can get a notification while it already unregistered himself. This happens if the manager (collection, ..., give it a name) is iterating over the collection of listeners and some listener unregisters himself during that iteration.
因此,删除侦听器"合同被打破.您认为这是一个问题吗?
So, the 'remove listener' contract is broken. Would you consider that as a problem?
推荐答案
即使您已检查侦听器是否仍已注册,在多线程环境中,您仍无法确定在调用它时仍已注册该侦听器.即使在非多线程情况下,标准实现也是在副本上工作.
Even if you checked the listener was still registered, in a multithreaded environment you still couldn't be sure it was still registered by the time you call it. Even for non-multithreaded situations, the standard implementation is to work on a copy.
这篇关于删除侦听器后,可以再在该侦听器上调用该事件一次吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!