Immutability with own by reference

View: New views
2 Messages — Rating Filter:   Alert me  

Immutability with own by reference

by Ashley Williams :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi,

Just a quick one: if my class contains some sort of modifiable list then usually it would be considered mutable. But is that the case if my class doesn't actually own the lifecycle of that list? So for example if that list has "owned by reference" semantics and was supplied externally by some parent of my class and the list that it writes its results to.

@Immutable??
class MyClass implements SomeCallback {
    List someList;

    MyClass(List someList) {
        this.someList = someList;
    }

    void onMessage(Message msg) {
        someList.add(msg);
    }

}

My hunch is that MyClass is immutable because the state is recorded in whichever class owns someList and we are just holding a handy reference to it. Is this reasoning sound?

Many thanks
- Ashley

---

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

Please refer to http://www.db.com/en/content/eu_disclosures.htm for additional EU corporate and regulatory disclosures.


_______________________________________________
Concurrency-interest mailing list
Concurrency-interest@...
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

Parent Message unknown Re: Immutability with own by reference

by Ashley Williams :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi Anton,

I'm aware of the pitfalls you mentioned, but the code I hand-typed is more for illustrative purposes -
you can assume the final keyword and also some better data structure such as ConcurrentHashMap.
It's the effects of the ownership semantics I'm more interested in, i.e. own by value versus own by reference.

- Ashley

Anton Maximov <amaximov@...> wrote on 12/10/2009 12:51:44:

> i would declare someList reference as final to ensure safe publication
> and initialization safety (JCIP 3.5.1 and 3.5.2).
>
> also, you are mutating the structure of the list passed to you without
> any synchronization; unless your List implementation itself takes care
> of synchronization internally, you are asking for trouble.
>
> -a
>
> On Mon, Oct 12, 2009 at 3:51 AM, Ashley Williams <ashley.
> williams@...> wrote:
> >
> > Hi,
> >
> > Just a quick one: if my class contains some sort of modifiable list then
> > usually it would be considered mutable. But is that the case if my class
> > doesn't actually own the lifecycle of that list? So for example ifthat list
> > has "owned by reference" semantics and was supplied externally by some
> > parent of my class and the list that it writes its results to.
> >
> > @Immutable??
> > class MyClass implements SomeCallback {
> >     List someList;
> >
> >     MyClass(List someList) {
> >         this.someList = someList;
> >     }
> >
> >     void onMessage(Message msg) {
> >         someList.add(msg);
> >     }
> >
> > }
> >
> > My hunch is that MyClass is immutable because the state is recorded in
> > whichever class owns someList and we are just holding a handy reference to
> > it. Is this reasoning sound?
> >
> > Many thanks
> > - Ashley
> >
> > ---
> >
> > This e-mail may contain confidential and/or privileged information. If you
> > are not the intended recipient (or have received this e-mail in error)
> > please notify the sender immediately and delete this e-mail. Any
> > unauthorized copying, disclosure or distribution of the material in this
> > e-mail is strictly forbidden.
> >
> > Please refer to http://www.db.com/en/content/eu_disclosures.htm for
> > additional EU corporate and regulatory disclosures.
> >
> > _______________________________________________
> > Concurrency-interest mailing list
> > Concurrency-interest@...
> > http://cs.oswego.edu/mailman/listinfo/concurrency-interest
> >
> >


---

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

Please refer to http://www.db.com/en/content/eu_disclosures.htm for additional EU corporate and regulatory disclosures.


_______________________________________________
Concurrency-interest mailing list
Concurrency-interest@...
http://cs.oswego.edu/mailman/listinfo/concurrency-interest