small medium large xlarge

30 Mar 2016, 22:39
Tianxiang Xiong (1 post)

Near the end of Chapter 2, Day 1, there is the following regarding using defensive copy to avoid calling alient methods:

This change also fixes another bug that isn’t related to concurrency—a listener can now call removeListener() within its onProgress() method without modifying the copy of listeners that’s mid-iteration.

I can see why this is true, but when would this ever be useful?

If a listener calls removeListener(), it probably wants to remove itself or another listener from listeners, right? If it removes a lister from listenersCopy, then it likely failed to do what it intended.

04 Apr 2016, 17:35
Paul Butcher (37 posts)

Take another look at the source; listenersCopy is only used within updateProgress(). The removeListener() function always works on listeners. This is, in fact, the point; if a listener calls removeListener(), it will work on listeners, even though updateProgress() is iterating through listenersCopy.

Does that make sense?

You must be logged in to comment