wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes synchronized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "synchronized" Watch "synchronized" New topic
Author

synchronized

K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71
i have problems understnding synchronized access to methods.
I wrote the following code to print values one after the another(that is A followed by B followed by C). But i am not getting it right.

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You must synchronize on ONE unique object. In your example you are synchronizing on THREE different objects. If you can create an object which is shared by the three thread objects and synchronize on that you will get the result you want. You could pass the shared object into the Test constructor as an additional parameter. You could also synchronize on the Test class object itself.
[ June 23, 2004: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71
Thanks for replying.
What modifications do i need to make in my code to make it work?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I've updated my last post to give you a clue . Let us know if you still have a problem.
K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71
Thanks Barry
I did the following but don't know why its working.
I synchronized only t2 but even other 2 display results as if they were also synchronizde.

Chris Hani
Ranch Hand

Joined: Jun 03, 2004
Posts: 42
I also want to know what happens in the replied code. It seems like when t finished, it is guaranteed t1 is selected before t2. As far as I know the the thread scheduling is totally upto platform.

I am looking forward to another explaination.

sung.
[ June 24, 2004: Message edited by: Sung Kum Her ]
K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71
Originally posted by Sung Kum Her:
I also want to know what happens in the replied code. It seems like when t finished, it is guaranteed t1 is selected before t2. As far as I know the the thread scheduling is totally upto platform.

I am looking forward to another explaination.

sung.

[ June 24, 2004: Message edited by: Sung Kum Her ]


The same reason i couldn't understand why its working.
Chris Hani
Ranch Hand

Joined: Jun 03, 2004
Posts: 42
By the way, this is my solution. Welcome to be criticised
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Here's my version of the original problem. Read the comments.
Sorry but I don't have time to write more at the moment, got to do some work.



On my computer it outputs:
Robert Miller
Ranch Hand

Joined: Jun 18, 2004
Posts: 56
Any statements enclosed by

will be executed only after the thread gets a lock on object a. Such a block has no effect on a block such as

unless it happens that a==b. This is because each Java object has its own lock.

Therefore, in order for two blocks of code to be synchronized with each other, they must synchronize on the same object. That's why having every thread synchronize on the same thread object works.

Robert
 
wood burning stoves
 
subject: synchronized
 
Similar Threads
Threads Synchronization (Urgent)
Threads: synchronizing a block of code (K&B 1.5)
deadlock?
Thread behavior with synchronized method
Why this synchronization doesn't work