• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is this method thread-safe ?  RSS feed

 
Jay Damon
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class that is defined as an Enumerated Type, i.e. there are only 6 instances created. In order to interface cleanly with some legacy code, I want to add an instance method to this class similar to the one shown below. The instance method does NOT change the class in any way; it serves merely to make a comparison (which is a little more involved than shown here) and return a primitive boolean value. I think I know the answers but I'm looking for some confirmation. My questions are:

1. Is this method thread-safe?
2. Are there any threading issues I should be concerned with?
3. Should I add a synchronized keyword to this method declaration?

 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If getType() returns a constant (if this is a standard Java 5 enum, then it does) then there are no threading issues here. The answers are "yes", "no", and "no."
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an aside, notice that



can be shorter written as

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!