Here is a reference to double-checked locking.  Many people have tried
to get around synchronization during lazy initialization - AFAIK, none
have succeeded.  With the new memory model in Java5, you can get away
with just volatile, which is like half a synchronization (a read
barrier + a write barrier).

http://www.google.com/search?q=double+checked+locking

-Yonik
http://incubator.apache.org/solr Solr, the open-source Lucene search server


On 5/9/06, yueyu lin <[EMAIL PROTECTED]> wrote:
In java, call a synchronized function in a synchronized block, if they have
the same mutex object, nothing will happen.
If they have different mutex objects, something may be screwed up.

On 5/10/06, Yonik Seeley <[EMAIL PROTECTED]> wrote:
>
> Yueyu Lin,
>
> Your patch below looks suspiciously like the double-checked locking
> anti-pattern, and is not guaranteed to work.
> There really isn't a way to safely lazily initialize without using
> synchronized or volatile.
>
> -Yonik
> http://incubator.apache.org/solr Solr, the open-source Lucene search
> server
>
> On 5/9/06, yueyu lin <[EMAIL PROTECTED]> wrote:
> > Yes, the modification is still synchronized and the first thread will be
> > responsible for reading first. And then other threads will not read and
> the
> > synchronization is unnecessary.
> > private void ensureIndexIsRead() throws IOException {
> >     if (indexTerms != null)                       // index already read
> >       return;                                     // do nothing
> >     synchronized(this){
> >         System.out.println("Read [EMAIL PROTECTED]@");
> >         if(indexTerms != null){
> >             System.out.println ("Someone read it.return-_-");
> >             return ;
> >         }
> >         readIndex ();
> >     }
> >   }
> >
> >   private synchronized void readIndex() throws IOException{
> >       Term[] m_indexTerms = null;
> >       try {
> >           int indexSize = (int)indexEnum.size;        // otherwise read
> > index
> >           m_indexTerms = new Term[indexSize];
> >           indexInfos = new TermInfo[indexSize];
> >           indexPointers = new long[indexSize];
> >
> >           for (int i = 0; indexEnum.next(); i++) {
> >             m_indexTerms[i] = indexEnum.term();
> >             indexInfos[i] = indexEnum.termInfo();
> >             indexPointers[i] = indexEnum.indexPointer;
> >           }
> >         } finally {
> >             indexEnum.close();
> >             indexEnum = null;
> >             indexTerms = m_indexTerms;
> >         }
> >   }
> >
> > That's a small trick I learned when I was developing a busy stock
> system.
> >
> > If the method ensureIndexIsRead() is synchronized, it will be blocked
> for a
> > while, although even 2 lines codes.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


--
--
Yueyu Lin



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to