• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

synchronized

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, this is my solution. Welcome to be criticised
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic