• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Thread Synchronization Doubt?

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

The following is from the K&B book. Page 720.


The explanation given as, "Notice in line 6 the code synchronizes itself with the object b- this is because in order to call wait() on the object. ThreadA must own a lock on b. For a thread to call wait() or notify(), the thread has to be the owner of the lock for that object.

So from my understanding, at line 6 which is in ThreadA, when we say synchronized(b), it means that ThreadA has locked ThreadB. Is that what I can infer?? This may sound dumb and stupid, but ranchers, please help me understand this.

Thanks in advance.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

Anyone please help me with the above code please???
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are correct. When you say synchronized(b) you are locking onto the instance of ThreadB (not the class itself). So while your thread is in that synchronized block, no other thread can access synchronized methods of that b instance, however, they still can run un-synhronized methods of that class.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jothi, did you forget something in that ThreadB class? You can't just call notify like that. You are going to get an IllegalMonitorStateException in thread B.
[ December 16, 2006: Message edited by: Barry Gaunt ]
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Barry,

I forgot the synchronized block.
 
reply
    Bookmark Topic Watch Topic
  • New Topic