Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #2406

Re: Safety Of Non-Synchronized Collections

Newsgroups comp.lang.java.help
Date 2013-01-02 11:12 -0800
References <kbvhvc$le1$1@dont-email.me> <kbvubp$4mr$1@dont-email.me>
Message-ID <6fa66d26-85c8-46df-97f3-37d8fff69222@googlegroups.com> (permalink)
Subject Re: Safety Of Non-Synchronized Collections
From Lew <lewbloch@gmail.com>

Show all headers | View raw


markspace wrote:
> Steve wrote:
>> In other words, I got the message "Use the new StringBuilder wherever
>> possible.", what I want to know is, how can I be sure it is possible?
> 
> I have to second the suggestion to obtain a good reference on the 
> subject and read it.  Java Concurrency in Practice is the only such good 
> reference I know, although the Java Language Specification is good too, 
> just a bit thick.
> 
> 
> 
> I kind of disagree with Lew's rule about final fields and immutability. 

What you show lacks immutability.

>   It's too easy to mess up if you don't what you are doing.  The 

Not if you keep things immutable. That's the defense against not knowing 
what you're doing.

> following seems to fit Lew's brief description, but it isn't immutable, 

Therefore does not seem to fit my definition.

You confuse me.

> or thread safe:

I did say "immutable".

If you don't know what that is, either learn or give up programming.

> public class ImmutableStringBuilder {
>    private final StringBuilder sb;
> 
>    public ImmutableStringBuilder( StringBuilder sb ) {
>      this.sb = sb;
>    }
> }
 
> So it's hard to catch errors like this if you don't really understand 
> Java's memory model.  (Memory model = multi-threading, for all intents 
> and purposes.)  Check out the Memory Model section of the JLS, and get 
> JCiP.  It's really the only way.

I agree, but many of the subtleties of multi-threaded programming in Java are 
avoided by the use of immutable objects. Yes, you do have to understand what 
immutable means. Thank you for illustrating that.

But you don't have to understand absolutely everything about concurrent programming 
to get that much right. Nor do you have to be a master of concurrent programming, 
not that that's really possible, to do any.

I read through JCIP and many of Brian Goetz's excellent articles on IBM Developerworks 
about Java concurrency. The /happens-before/ relationship definitely helps one to reason 
correctly about it. But it didn't make me a master, despite decades of experience in 
concurrent programming. But I'm better than average.

Even with knowledge, it's just so much easier to rely on immutability. Lazy instantiation 
is a false bargain.

-- 
Lew

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Safety Of Non-Synchronized Collections Steve <tinker123@gmail.com> - 2013-01-01 15:46 -0500
  Re: Safety Of Non-Synchronized Collections Knute Johnson <nospam@knutejohnson.com> - 2013-01-01 14:18 -0800
    Re: Safety Of Non-Synchronized Collections Lew <lewbloch@gmail.com> - 2013-01-01 15:43 -0800
  Re: Safety Of Non-Synchronized Collections markspace <markspace@nospam.nospam> - 2013-01-01 16:17 -0800
    Re: Safety Of Non-Synchronized Collections Lew <lewbloch@gmail.com> - 2013-01-02 11:12 -0800
      Re: Safety Of Non-Synchronized Collections markspace <markspace@nospam.nospam> - 2013-01-02 12:56 -0800
        Re: Safety Of Non-Synchronized Collections Lew <lewbloch@gmail.com> - 2013-01-02 17:46 -0800
  Re: Safety Of Non-Synchronized Collections Roedy Green <see_website@mindprod.com.invalid> - 2013-01-03 05:59 -0800
    Re: Safety Of Non-Synchronized Collections Lew <lewbloch@gmail.com> - 2013-01-03 15:30 -0800
      Re: Safety Of Non-Synchronized Collections Roedy Green <see_website@mindprod.com.invalid> - 2013-01-09 12:17 -0800
        Re: Safety Of Non-Synchronized Collections Lew <lewbloch@gmail.com> - 2013-01-09 12:56 -0800
          Re: Safety Of Non-Synchronized Collections Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-01-09 13:10 -0800
            Re: Safety Of Non-Synchronized Collections Lew <lewbloch@gmail.com> - 2013-01-09 14:51 -0800
              Re: Safety Of Non-Synchronized Collections Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-01-09 15:22 -0800
                Re: Safety Of Non-Synchronized Collections Lew <lewbloch@gmail.com> - 2013-01-09 16:26 -0800
                Re: Safety Of Non-Synchronized Collections Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-01-09 16:42 -0800
                Re: Safety Of Non-Synchronized Collections Lew <lewbloch@gmail.com> - 2013-01-09 16:54 -0800
                Re: Safety Of Non-Synchronized Collections Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-01-10 10:30 -0800
                Re: Safety Of Non-Synchronized Collections Lew <lewbloch@gmail.com> - 2013-01-10 10:58 -0800
                Re: Safety Of Non-Synchronized Collections markspace <markspace@nospam.nospam> - 2013-01-09 16:50 -0800
                Re: Safety Of Non-Synchronized Collections Lew <lewbloch@gmail.com> - 2013-01-09 16:59 -0800
                Re: Safety Of Non-Synchronized Collections markspace <markspace@nospam.nospam> - 2013-01-10 08:18 -0800
                Re: Safety Of Non-Synchronized Collections Lew <lewbloch@gmail.com> - 2013-01-10 10:49 -0800
                Re: Safety Of Non-Synchronized Collections Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2013-01-10 13:37 +0200
                Re: Safety Of Non-Synchronized Collections Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-10 09:27 -0500
                Re: Safety Of Non-Synchronized Collections Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2013-01-10 17:14 +0200
                Re: Safety Of Non-Synchronized Collections Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-10 11:44 -0500
                Re: Safety Of Non-Synchronized Collections Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2013-01-10 23:41 +0200
                Re: Safety Of Non-Synchronized Collections Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-10 17:14 -0500
                Re: Safety Of Non-Synchronized Collections Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2013-01-11 09:10 +0200
                Re: Safety Of Non-Synchronized Collections Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-11 08:25 -0500

csiph-web