The Artima Developer Community
Sponsored Link

Legacy Design Forum
Thread Safety



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


Thread safe coding guidelines

Posted by Mahesh on August 28, 2000 at 6:00 PM


We need always make code inside RMI-Servers and servlets thread safe, right ? because they will invariably used in a multithreaded environment. Now how can we verify that a given method is indeed thread safe or does not require any further measures for thread safety or does not need thread-safety considerations at all?

I came up with an approximate guideline - any method with following characteristics is inherently thread-safe:

- uses only method local variables and parameters passed to it
- does not update any static variables
- does not update any instance variables
- does not access any external resources (file, database connection etc.)
- calls only synchronized methods or methods which conform to all the above rules (both of which are thread-safe themselves

Is this list exhaustive or do I need to check anything more before walking over a method as thread-safe ?

Mahesh H.


Sponsored Links

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