File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized

 
K Anshul
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
K Anshul
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying.
What modifications do i need to make in my code to make it work?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've updated my last post to give you a clue . Let us know if you still have a problem.
 
K Anshul
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, this is my solution. Welcome to be criticised
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic