• Post Reply Bookmark Topic Watch Topic
  • New Topic

threadsafe class requirements  RSS feed

 
Mandy Bowman
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

a question about threadsafe classes. In order to make a class threadsafe is it enough to make all the methods in it synchronised or is there more that you need to do?
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Synchronizing all the methods in a Class works, but it is overkill. Its like having to go through the security check points at an airport for every access to each method.

There are many tutorials on concurrency. Try one.
Concurrency Tutorial
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Synchronizing all the methods may be overkill. But in other cases, it may be exactly right. And in others, it's not really enough. For example Vector has all its methods synchronized, but despite the way it's often advertised, it's often not thread-safe, they way people actually use it. That's because it usually requires some additional synchronization to prevent another thread from cutting in between two different method calls to the Vector.


This is a recurring problem for any class where you need to call multiple methods in sequence in order to accomplish something - synchronizing the methods is not enough. Often it's necessary for the code that uses the class to do the synchronization. Unless you can design your class' method so that the do what you need in just one method call.
 
Mandy Bowman
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim,

I must say I never thought of what you just decribed. It was very educational. It makes me wonder whether the Vector class is Thread-safe at all then by definition.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are sure that the object of the class that is under consideration will never have a chance to be accessed by multiple threads then synchronization would simply be overhead.

Consider objects stored as a request attribute , you do not need to do anything as no other thread can access that object (Under normal conditions).

In case you have a object that will be subjected to multiple threads then you can consider making the methods synchronized.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Mandy Bowman]: I must say I never thought of what you just decribed. It was very educational. It makes me wonder whether the Vector class is Thread-safe at all then by definition.

Keep wondering, test everything and consider the following:

Without correct synchronization, very strange, confusing and counterintuitive behaviors are possible.

Counter-intuitive means "it just doesn't make sense", which is a terrible thing to say after your client's machine chewed up some data.

In order to make a class threadsafe it is enough to make all the methods in it synchronised, there is nothing more that you need to do - but what about other classes that use a class ? What about b class, and c class and d class that use an instance of a class, if they access a variable of a? If you can make all your methods static, it may be effective if they are all synchronized.

It would help if you can come up with some illustrative code.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By synchronizing a method I think the intent is to write synchronized before a method. If that's the case then also it leaves room for static variables in a non static method. So I would say just by writing synchronized before a method would not make it thread safe.
[ June 23, 2007: Message edited by: Anupam Sinha ]
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Anupam Sinha:] By synchronizing a method I think the intent is to write synchronized before a method.

The semantics of the Java programming language allow compilers and microprocessors to perform optimizations that can interact with incorrectly synchronized code in ways that can produce behaviors that seem paradoxical.

It is the intent of the word synchronized to create a monitor that can:
  • t - identifiy the thread performing the action
  • k - the kind of action
  • u - the variable or monitor involved in the action.
  • v - an arbitrary unique identifier for the action

  • in the code produced by the compiler.

    The placing of the keyword synchronized thwarts reorderings that come from two cooks in the kitchen.
     
    Jim Yingst
    Wanderer
    Sheriff
    Posts: 18671
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Anupam, you are entirely correct - just synchronizing methods may not be enough. Static variables in instance methods, or instance valriables in static methods, may require additional protection. It's also possible for an instance method to access data from another instance, which may also require additional protection.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!