On Thu, Jun 7, 2012 at 1:27 AM, Howard Lewis Ship <hls...@gmail.com> wrote:

> You can even omit synchronized and volatile IFF:
> - only a single shared field is updated
> - it is ok for a race condition to exist that would create the value
> on multiple threads
> - (I learned this by getting schooled on the subject by Rich Hickey)
>
> There are many things in Tapestry that don't fit this model.
> Sometimes the value being lazily created is so expensive, it would be
> invalid to allow multiple threads to create it.  In other cases, more
> than one shared field is updated.
>


Hm, but the second check inside 'synchronized' block is supposed to avoid
creating value on multiple threads. For example:

    private volatile Messages messages;

    public Messages getMessages()
    {
            if (messages != null)
            {
                return messages;
            }

           synchronized (this) {
                   if (messages != null)
                   {
                       return messages;
                   }

                   messages = elementResources.getMessages(componentModel);
// <--- only called if no other thread did this before
           }

            return messages;
    }




Cezary

Reply via email to