The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
October 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

RE: Thread safefy

Posted by Grog on October 19, 2001 at 3:03 AM

Thanks Matt,

I had come to pretty much the same conclusion, just good to hear someone else's opinion. I am relying on the logger being correctly syncronised, and if not I will just synchronise my methods.

Thanks again,

Grahame

> I would guess in this case you are probably okay. It would be more worriesome if you were adding and removing log items, relying on positions of log items in the list and so on. As you are only adding items and doing it in a fairly atomic way, there is not much chance for things to go awry, unless the underlying Logger class has a really goofy implementation.

> Conceivably, if your Logger class is writing to a printer, then you could get silly results like this printing out:
>


> Error 1:
> Warning 1:
> "warning message"
> "error message"
>

> If one thread got a slice in the middle of the other's activities. It may be that the underlying Logger class is already synchronized.

> In any case it looks like it would be pretty easy to create a few test threads that pump in a bunch of warnings and errors into the log and see how it works.

> On the other hand, if you don't have time to do the above test, and this is keeping you up at night, it wouldn't be a terrible thing to add "synchronized" to your own methods (the small performance hit of being synchronize is probably infinitesimal compared to the time that it takes to actually log a message).

> - mfg






Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us