简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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