• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using jcip annotation  RSS feed

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm currently using jcip annotations to annotate the thread safety of my classes.


In the line marked **, I am delegating to a thread safe ConcurrentLinkedQueue. Sorry for being pedantic here, but what would be
the appropriate entry for the @GuardedBy annotation:

@GuardedBy( value="queue" ) ?

Obviously the instance of ConcurrentLinkedQueue depends on some internal
locks. This seems to mean that in the method offer, I'm actually doing:



Your thoughts ?

Pho
 
Santhosh Kumar
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to answer the point, yes, it should be name of the field you are using to make the method threadsafe.

However I have couple of comments about usage of @GuardedBy

1. net.jcip.annotations are intended to be used as documentation and annotations themselves doesn't enforce any thread safety or concurrency control.

2. While @Immutable, @NotThreadSafe and @ThreadSafe do add some value but I doubt the same about @GuardedBy reason being, annotations may get documented in the Javadoc and hence public might be able to see how the method is made thread-safe. This kind of violating the encapsulation paradigm of OOP. Let's say you guarded a method using one lock and later you change the implementation to something else for whatever reason? So suggest not to use the @GuardedBy and put a comment inside the code if you want to make it clear as to why a lock is used/not used.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, I'm sorry I didn't notice this thread back when it was first posted.

[B][Pho]: This seems to mean that in the method offer, I'm actually doing:
[/B]

No, it actually identifies a lock which you are expected to hold before accessing the field or method which has the @GuardedBy annotation. If you have this annotation:

then that implies that whenever any other code calls offer(), it should hold the lock on foo. E.g.

The annotation doesn't say anything about what sort of locking may be done within the method - it's all about what lock is required outside the method. Thus, the annotation isn't appropriate for the offer() method at all, in your code above. The caller doesn't need any lock at all in order to call the method, and there's no need for an annotation implying they do.

[Santhosh]: 1. net.jcip.annotations are intended to be used as documentation and annotations themselves doesn't enforce any thread safety or concurrency control.

True, but it's also possible for other tools such as IDEs to make use of these annotations to issue warnings and the like. FindBugs does this; I don't know about other tools.

[Santhosh]: 2. While @Immutable, @NotThreadSafe and @ThreadSafe do add some value but I doubt the same about @GuardedBy reason being, annotations may get documented in the Javadoc and hence public might be able to see how the method is made thread-safe. This kind of violating the encapsulation paradigm of OOP. Let's say you guarded a method using one lock and later you change the implementation to something else for whatever reason? So suggest not to use the @GuardedBy and put a comment inside the code if you want to make it clear as to why a lock is used/not used.

I disagree - this seems to be based on an incorrect understanding of what @GuardedBy is for, much like Pho's misunderstanding. If @GuardedBy is on a non-private field or method, it means that other coders do need to know about what sort of locking is needed to access the field or method. If it's on a private field or method, the annotation won't appear in generated JavaDoc anyway, unless you specifically ask for javadoc of private members - and in that unlikely case, you're exposing lots of normally hidden implementation details. Even without the javadoc though, it can be helpful to use @GuardedBy on private members, as an aid to programmers working within that particular class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!