• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronization  RSS feed

 
Mike Cronin
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Can someone please set me straight on a few synchronization concepts?
Are method variables thread-safe? I thought I read that method variables are thread-safe, but I am not yet fully convinced this is true.
I read servlet "service requests" get their own instance, and because of this are thread-safe. Although I have encountered threading issues in servlet doGet and doPost methods that seem to discredit that statement. "Service requests" are synonymous with doGet and doPost methods. Isn't that correct?
When a thread enters a synchronized method, are all synchronized methods within that class locked?
Can someone please set me straight on these issues?
Can anyone recommend solid documentation, books or links regarding synchronization?
Thanks so much!!!
Mike Cronin :roll:
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Cronin:
Hi all,
Can someone please set me straight on a few synchronization concepts?
Are method variables thread-safe?

Also known as local variables, yes they are. Each thread has its own call stack, and since local variables are created on the stack, there's no way for them to interfere with each other.

When a thread enters a synchronized method, are all synchronized methods within that class locked?

If you mean that all the methods have an explicit synchronzied modifier in their delcaration, then yes. The thread that acquires the lock for the object will run that method, and any other threads attempting to acquire the same object lock in order to run that method, or any other method in the same class that's also synchronized, will block until the first thread releases the object lock.
[ April 18, 2002: Message edited by: Rob Ross ]
 
Mike Cronin
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a bunch Rob!
I guess the more I deal with synchronization the better I'll understand the real issues.
Right now I just don't feel confident enough when it comes to synchronization. Maybe someday when I grow up I'll have a better grasp on this elusive issue
Thanks again for your valued reply!

Best Regards!!!
Mike Cronin
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anal point of accuracy
All objects are created on the heap. Even if they are created in a method. But yes, the references are on the stack and essentially they are safe too.
 
Mike Cronin
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks CLG!
Any and all comments are most welcome
Mike Cronin
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!