commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jerome Jacobsen" <jerome.jacob...@gentootech.com>
Subject RE: [Logging] Thread safety guaruntee of Log classes?
Date Wed, 11 Dec 2002 22:49:24 GMT
>
> Declaring the Log variable static or not has zero effect on whether it is
> threadsafe or not -- it only determines whether or not the Log instance is
> shared across all instances of the declaring class.
>
> Craig
>

Huh?  Declaring the Log instance static means that it better be thread-safe.
Otherwise if an instance of MyComponent is accessed from Thread1 and another
instance of MyComponent is simultaneously accessed from Thread2 then you've
got problems.  That is, unless you access your Log attribute like this in
MyComponent:

synchronized(log) {log.info("Hello World");}

But who wants to do that?

Imagine an unsophisticated LogImpl that updates some state in an internal
HashMap on every call to a logging method.  Say the HashMap key is the log
level and the HashMap value is an ArrayList of Strings (log messages).  Do
you see what I mean?  Unless a LogImpl instance maintains its state in a
thread-safe manner it cannot be shared accross threads unless explicit
synchronization is done by those threads.  Struts does no such
synchronization.


Mime
View raw message