lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Robert Engels" <reng...@ix.netcom.com>
Subject RE: Multiple threads searching in Lucene and the synchronized issue. -- solution attached.
Date Wed, 10 May 2006 03:43:58 GMT
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:yseeley@gmail.com]
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 <popeyelin@gmail.com> 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 index@--@");
>         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: java-dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-dev-help@lucene.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: java-dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-dev-help@lucene.apache.org


Mime
View raw message