A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Win a copy of
Java 8 in Action
this week in the
Developer Certification (SCJD/OCMJD)
Joined: Apr 04, 2010
Oct 09, 2010 10:03:03
I just want to nail down my understanding of synchronization, I've got two questions:
Method A Synchronized
Method B (not marked as synchronized)
Method C (not marked as synchronized)
Methods B & C are in different classes
If method A is synchronized and method A calls B, which in turn calls C, does the whole chain become synchronized(A,B&C)?
My program does this, but B is only called by A. I assume this is safe and Synchronized?
Q2/ Is there a difference between the following. One uses Synchronization, the other locks. To me they both do the same thing, but example 1 is cleaner and less error prone:
public synchronized updateRoom(int recNo, String data)
public updateRoom(int recNo, String data) lock.lock(); other code lock.unlock();
SCJP 6 , OCMJD 6 ,
Roel De Nijs
Joined: Jul 19, 2004
Oct 09, 2010 17:27:36
Q1/ it depends. If method B (and C) are called from other methods than method A, methods B and C will not be synchronized.
Q2/ if you want to synchronize your complete method, then example 1 certainly does the job in the easiest way possible.
, SCJP (
I agree. Here's the link:
subject: synchronization query
pls validate my locking strategy - all inputs are g8ly appreciated. (URLyBird)
Methods in RMI and Multithreading
delete() and unlock()
My locking approach...comments please
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2014