How to protect dynamic lookup content when it is changing?

"rsobies" <[email protected]>
Newsgroups gmane.comp.java.netbeans.modules.openide.devel
Message-ID <[email protected]>
Boris Heithecker wrote:
> 
> B: no listeners, you just want the lookup to never be empty -> you could reverse the order of invocation (instanceContent.add(newKey) before instanceContent.remove(oldKey))
> 


yes. that should do the work. so simple. thanks :)


you said that getLookup is thread safe so i wonder if i lock instancontent in setNewKey() like this:



Code:

public void setNewKey(SomeKey key) { 

        Collection<? extends SomeKey> lookupAll = getLookup().lookupAll(SomeKey.class); 

        synchronized(instanceContent)
            for (SomeKey item : lookupAll) { 
                 instanceContent.remove(item); 
            } 

//if getLookup.lookup(SomeKey.class) is invoked by another thread at this point, will it be blocked untill end of this synchronized block?
         }
 

        instanceContent.add(key); 
    }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.