lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "yueyu lin" <popeye...@gmail.com>
Subject Re: Multiple threads searching in Lucene and the synchronized issue. -- solution attached.
Date Wed, 10 May 2006 02:11:40 GMT
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 <yseeley@gmail.com> 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 <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
>
>


--
--
Yueyu Lin

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message