IntelliJ Java IDE
The moose likes Threads and Synchronization and the fly likes Synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Synchronization" Watch "Synchronization" New topic
Author

Synchronization

Mike Cronin
Greenhorn

Joined: Apr 17, 2002
Posts: 25
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

Joined: Jan 07, 2002
Posts: 2205
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 ]

Rob
SCJP 1.4
Mike Cronin
Greenhorn

Joined: Apr 17, 2002
Posts: 25
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

Joined: Oct 05, 2001
Posts: 1158

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

Joined: Apr 17, 2002
Posts: 25
Thanks CLG!
Any and all comments are most welcome
Mike Cronin
 
 
subject: Synchronization
 
Threads others viewed
which part of the servlet runs in new threads
Servlet synchronization question.....
About SingleThreadModel
please reply urgently!!!
Thread Safe Instance Variables
IntelliJ Java IDE