I wrote a test case to test the performance (assuming that it worked, but
based on reading the double-checked articles I understand the dilemma).

Using 30,000,000 simple iterations and 2 threads: (note this is on a single
processor machine).

sync time = 39532
unsync time = 2250
diff time = 37282
diff per iteration = 6.213666666666667E-4

So it saves .0006 ms per invocation of the method.

I honestly doubt this is the cause of any performance bottleneck.

I attached the test code if you are interested.



-----Original Message-----
From: yueyu lin [mailto:[EMAIL PROTECTED]
Sent: Tuesday, May 09, 2006 11:32 PM
To: java-dev@lucene.apache.org
Subject: Re: Multiple threads searching in Lucene and the synchronized
issue. -- solution attached.


I met these problem before indeed.The compiler did something optimized for
me that was bad for me when I see the byte-codes.
 When I'm using a function local variable, m_indexTerms and in JDK1.5.06, it
seems ok.
Whether it will break in other environments, I still don't know about it.
On 5/10/06, Yonik Seeley <[EMAIL PROTECTED]> wrote:
>
> On 5/9/06, Robert Engels <[EMAIL PROTECTED]> wrote:
> > I am fairly certain his code is ok, since it rechecks the initialized
> state
> > in the synchronized block before initializing.
>
> That "recheck" is why the pattern (or anti-pattern) is called
> double-checked locking :-)
>
> -Yonik
> http://incubator.apache.org/solr Solr, the open-source Lucene search
> server
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


--
--
Yueyu Lin
public class TestSyncCosts {
    static Object test = null;
    
    static final int NTHREADS = 2;
    static final int NITERATIONS = 30000000;
    static int creates = 0;
    
    public static void main(String[] args) throws Exception {
        
        Thread[] threads = new Thread[NTHREADS];
        
        for(int i=0;i<NTHREADS;i++) {
            threads[i]=new Thread(new TestSyncRunnable());
        }
        long stime = System.currentTimeMillis();
        for(int i=0;i<NTHREADS;i++) {
            threads[i].start();
        }
        for(int i=0;i<NTHREADS;i++) {
            threads[i].join();
        }
        
        long time0 = System.currentTimeMillis() - stime;
        
        for(int i=0;i<NTHREADS;i++) {
            threads[i]=new Thread(new TestUnSyncRunnable());
        }
        stime = System.currentTimeMillis();
        
        test = null;
        
        for(int i=0;i<NTHREADS;i++) {
            threads[i].start();
        }
        for(int i=0;i<NTHREADS;i++) {
            threads[i].join();
        }
        long time1 = System.currentTimeMillis() - stime;
        
        System.out.println("sync time = "+time0);
        System.out.println("unsync time = "+time1);
        System.out.println("diff time = "+(time0-time1));
        System.out.println("diff per iteration = "+(time0-time1)/(((double)NITERATIONS)*NTHREADS));
        
        if(creates != 2) {
            System.out.println("oops, creation failure");
        }
    }
    
    static synchronized void synchronizedCheck() {
        if(test==null) {
            test = new Object();
            creates++;
        }
    }
    static void unsynchronizedCheck() {
        if(test!=null)
            return;
        synchronized(TestSyncCosts.class){
            if(test!=null)
                return;
            test = new Object();
            creates++;
        }
    }
    
    static class TestSyncRunnable implements Runnable {
        public void run() {
            for(int i=0;i<NITERATIONS;i++) {
                synchronizedCheck();
            }
        }
    }
    static class TestUnSyncRunnable implements Runnable {
        public void run() {
            for(int i=0;i<NITERATIONS;i++) {
                unsynchronizedCheck();
            }
        }
    }
}

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

Reply via email to