File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes syncronization is a problem, is it not? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "syncronization is a problem, is it not?" Watch "syncronization is a problem, is it not?" New topic

syncronization is a problem, is it not?

Harvinder Singh
Ranch Hand

Joined: Feb 14, 2003
Posts: 90
Saturday, February 14, 2004
I wrote this code to prove
“In the synchronized block of statements,If instead of a this reference, the expression yielded a reference to another object, the lock associated with that object would be acquired before the thread continued.”
--create a thread in which pass a runnable object of the Funda() class
--run() method calls the synchOther() method which will get the lock on a object of Harry class(har) and goes to sleep for 10 seconds.
--Inside the main() thread now call the name method of the harry() class which should not run since the Thread is holding the lock on har object.

My problem is that in the main method as soon as I call the name() it runs. I hope that I am not making any basic mistakes.Plz make the necessary changes so that the code proves the aforesaid objective.

Hard work beats talent<br />when talent doesn't work hard.<p> - Tim Notke
Richard Quist
Ranch Hand

Joined: Feb 18, 2004
Posts: 96
According to the java language spec,
Acquiring the lock associated with an object does not of itself prevent other threads from accessing fields of the object or invoking unsynchronized methods on the object
(JLS - Synchronized statement
So I think what you need to do is make the name() method of Harry synchronized (or have it contain a synchronized block)

SCJP 1.4
I agree. Here's the link:
subject: syncronization is a problem, is it not?
It's not a secret anymore!