commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dominique Devienne <DDevie...@lgc.com>
Subject RE: [Logging] Thread safety guaruntee of Log classes?
Date Wed, 11 Dec 2002 23:01:11 GMT
Craig was just pointing out correctly that declaring your variable static or
not doesn't change much about thread-safety. The fact that you have a
per-instance log member doesn't make it thread-safe: that one instance can
still be accessed from different thread. True, this might yield less
're-entries' on the log non-static member, but that doesn't make it
thread-safe.

Is that a little clearer? --DD

-----Original Message-----
From: Jerome Jacobsen [mailto:jerome.jacobsen@gentootech.com] 
Sent: Wednesday, December 11, 2002 4:49 PM
To: Jakarta Commons Users List
Subject: RE: [Logging] Thread safety guaruntee of Log classes?

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