I am fairly certain his code is ok, since it rechecks the initialized state
in the synchronized block before initializing.

Worst case, during the initial checks when the initialization is occurring
there may be some unneeded checking, but after that, the code should perform
better since it will never enter a synchronized block.

I just doubt that this change makes any real difference if the IndexSearcher
is long-lived.

-----Original Message-----
From: Yonik Seeley [mailto:[EMAIL PROTECTED]
Sent: Tuesday, May 09, 2006 9:04 PM
To: java-dev@lucene.apache.org
Cc: Otis Gospodnetic
Subject: Re: Multiple threads searching in Lucene and the synchronized
issue. -- solution attached.


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]


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

Reply via email to