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]

Reply via email to