Re: How to protect dynamic lookup content when it is changing?

Boris Heithecker <[email protected]>
Newsgroups gmane.comp.java.netbeans.modules.openide.devel
Message-ID <CAL465RHg-ESGvAYfYk2b4JwYUWVgQsQ1odxZaA_bgMRsAaHQQw@mail.gmail.com>
I think I didn't understand all of your problem.
Is it

A: that instanceContent.remove(item) triggers a LookupEvent while the
lookup is empty -> in this case the listener could simply ignore the event
because a second one will be dispatched subsequently while processing
instanceContent.add(key)

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

C: ?

In principal, you can keep a reference also to the AbstractLookup which is
created in your constructor (you need a third constructor), but I think
that synchronizing on either instanceContent or the abstractLookup is no
good design, because it is deadlock prone.

Another option is passing an instanceof RequestProcessor to the instanceContent
(call "new InstanceContent(new RequestProcessor(...))") to dispatch lookup
events in a different thread.

A third option could be: instead of putting SomeKey itself in the lookup,
use a wrapper class and put a singleton instance of it - which is never to
be removed - in your lookup. E.g.:

public class CarReference extends AtomicReference<Car> {

    private final ChangeSupport cSupport = new ChangeSupport(this);

    void setAndNotify(Car newCar) {
        Car before = super.getAndSet(newCar);
        if (!Objects.equals(before, newCar)) {
            cSupport.fireChange();
        }
    }

    public void addChangeListener(ChangeListener listener) {
        cSupport.addChangeListener(listener);
    }

    public void removeChangeListener(ChangeListener listener) {
        cSupport.removeChangeListener(listener);
    }


}

Boris

2016-10-12 15:34 GMT+02:00 rsobies <[email protected]>:

> or if getLookup.lookup(SomeKey.class) is invoked, instancontent is locked?
> then i only  have to make synchronization in setNewKey
> am i right?
>
>
>
>
>


-- 
Boris Heithecker


Dr. Boris Heithecker
Lüneburger Str. 30
28870 Ottersberg
Tel.: 0 42 05/ 31 58 34
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.