简体   繁体   English

删除侦听器后,可以再在该侦听器上调用一次事件吗?

[英]When a listener is removed, is it okay that the event be called on that listener one more time?

Most people use a ConcurrentLinkedQueue or CopyOnWriteArrayList to collect listeners and notify them when something happens. 大多数人使用ConcurrentLinkedQueue或CopyOnWriteArrayList来收集侦听器,并在发生事件时通知它们。 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. 即使在非多线程情况下,标准实现也是在副本上工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM