That's the standard 'Singleton' pattern.  If you follow all the stuff about
Double-Checked locking, that's what they come up with too...

-----Burton 

-----Original Message-----
From: Trenton D. Adams [mailto:[EMAIL PROTECTED] 
Sent: Friday, December 02, 2005 1:12 PM
To: Log4J Users List
Subject: Re: log4j multithreading performance

Actually, a co-worker and I just came up with a possible solution.

public static Object getInstance()
{
   if (instance == null)
     createInstance();

   return instance;
}

private static synchronized void createInstance() {
   if (instance == null)
     instance = new Object();
}

So, we end up having the required synchronized method, but without the
normal performance hit.

Ok, I'm done now. :)

Curt Arnold wrote:
> 
> On Dec 1, 2005, at 8:39 PM, Trenton D. Adams wrote:
> 
>>
>> As a general rule, singleton methods should not be synchronized for 
>> performance reasons.  If this method is only called once in awhile, 
>> no big deal.  But, if it's called regularly, then it can be a  really 
>> big performance problem.  Does the log4j project have coding 
>> standards for such things?
>>
>> This particular singleton method should check the 
>> "_defaultLogMonitor" for null first.  If it is not null, it should 
>> return it.  If it is null, it should synchronize on some object, 
>> perhaps LF5Appender.class.  Then it should check it for null  again.
>> If it is not null, it should return it.  If it is null, it  should 
>> construct it.
>>
> 
> That idiom is not thread-safe but the explanation is too complicated  
> to go into here.  See "The Double-Checked Locking is Broken"  
> Declaration (http://www.cs.umd.edu/~pugh/java/memoryModel/
> DoubleCheckedLocking.html).  Also covered on pg 193 of "Effective  Java" 
> which I highly recommend.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 


--
Trenton D. Adams
Systems Analyst/Web Software Engineer
Navy Penguins at your service!
Athabasca University
(780) 675-6195
:wq!

__ 
    This communication is intended for the use of the recipient to whom it
    is addressed, and may contain confidential, personal, and or privileged
    information. Please contact us immediately if you are not the intended
    recipient of this communication, and do not copy, distribute, or take
    action relying on it. Any communications received in error, or
    subsequent reply, should be deleted or destroyed.
---

---------------------------------------------------------------------
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