Re: How to protect dynamic lookup content when it is changing?
Tim Boudreau <[email protected]>
| Newsgroups | gmane.comp.java.netbeans.modules.openide.devel |
|---|---|
| Message-ID | <CA+qecRNDfDjvPw57rADC2A8HN7sTdXKxRJBGR-ys-quPMQnMBw@mail.gmail.com> |
Lookup is designed to be thread-safe. So this should be a non-issue - calling code will either see the object or not. If you think this is the source of your problem, it is probably something else. So... what problem are you trying to solve? What is the sequence of events, and what about it suggests that synchronizing lookup access would help? If the problem is that some code expects an object to be in a Lookup, and it isn't necessarily there, the usual answer is to key enablement of whatever action results in that code off of the presence or absence of that object. If the problem is that loading / constructing the object can take a long time, Lookup may not be the right tool for the job (since it can be called from any thread and should not trigger long-running work), and you may want some asynchronous call that completes once loading is complete. That is not the same problem as synchronizing access, and you probably don't want to block the UI while it happens. -Tim On Tue, Oct 11, 2016 at 11:19 AM, Boris Heithecker < [email protected]> wrote: > Keep a reference to the InstanceContent and synchronize on it. It should > work as a common lock. > > Am 11.10.2016 13:33 schrieb "rsobies" <[email protected]>: > >> i would like to block accesing lookup content when another thread is >> puting into lookup. >> here is an example >> >> >> Code: >> >> public class MyNode extends AbstractNode { >> >> private InstanceContent instanceContent = new InstanceContent(); >> >> public MyNode(SomeKey key) { >> this(key, new InstanceContent()); >> } >> >> private CarNode(SomeKey key, InstanceContent ic) { >> super(Children.LEAF, new AbstractLookup(ic)); >> >> this.instanceContent = ic; >> instanceContent.add(key); >> } >> >> public void setNewKey(SomeKey key) { >> Collection<? extends SomeKey> lookupAll = >> getLookup().lookupAll(SomeKey.class); >> for (Object item : lookupAll) { >> remove(item); >> } >> >> //at this time lookup is empty, if another thread tries to get content, >> it gets null >> >> instanceContent.add(key); >> } >> } >> } >> >> >> >> >> is there an elegant way to block getLookup().lookup(SomeKey.class) >> during execution of setNewKey()? >> >> >> >> >> -- http://timboudreau.com