JDK-4078860 : API doc comments should describe the concurrency semantics of classes & methods
  • Type: Enhancement
  • Component: other-libs
  • Sub-Component: other
  • Affected Version: 1.2.0,5.0,6
  • Priority: P3
  • Status: Open
  • Resolution: Unresolved
  • OS: solaris_2.5,solaris_10,windows_xp
  • CPU: x86,sparc
  • Submitted: 1997-09-13
  • Updated: 2009-04-28
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Description
The class and method descriptions should describe whether the object can be operated on by multiple threads concurrently, or whether methods can be called concurrently by multiple threads.

This doc bug has a corollary javadoc tool bug 4078855 that states that javadoc should be modified so it does not print the keywords "synchronized"
or "native" in the method signatures.

It's important for the developer to know whether it's safe for multiple threads
to operate on an object concurrently,.  To this end, it is important for the general class description and method descriptions to describe whether the object is thread-safe or not.  However, this synchronization can be implemented either in public methods or in private methods, and the Java Platform API spec should not declare one to be preferred over the other.  A licensee should be able to achieve synchronization internally if they wish.  

Example 1:  java.util.Enumeration interface - A single Enumeration cannot be used by multiple threads concurrently.

Example 2:  java.util.HashTable class - An instance of HashTable cannot be
used by multiple threads concurrently.

Reason: Even assuming that Hashtable should be thread-safe, there's no reason that we should tell people that we're achieving this by synchronizing all of its exported methods.  We should reserve the right to synchronize internally at the bucket level, thus offering higher concurrency.  (Geeks take note: the auto-grow functionality of Hashtables may prevent us from taking advantage of this specific suggestion, but you get the idea.) - Josh Bloch

Comments
EVALUATION There is currently no known funding for Sun-exclusive implementation of wholesale enhancement of documentation with respect to thread safety. That leaves one obvious alternative. The book Java Concurrency in Practice discusses how to document the thread safety of an API. Perhaps a set of "best practices" could evolve from that. This topic is being explored by the JSR-166 expert group. A worthwhile goal would be a concrete proposal to share with the Java concurrency community via this list: http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest Modeling of best practices via updates to the java.util.concurrent source code seems like a good place to start this process. The more general effort would seem to call for an evolved heirarchy of code inspection and review process done primarily by the Java community and feeding into the master source code bases involved. And this kind of enhancement would not just be of benefit to the JDK: all Java code that might be used in a concurrent context could benefit. Efforts along these lines would naturally drive learning of a type that is vitally important to the future of the Java platform.
13-01-2006