• 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 Doubt

 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone expalin me the flow of this program??? What exactly happens when synchronized(b) is called? and what is synchronised(this)?
Thanks, in advance.




[ October 16, 2008: Message edited by: Abhi vijay ]

[edit]Add code tags. CR[/edit]
[ October 17, 2008: Message edited by: Campbell Ritchie ]
 
Marshal
Posts: 80093
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please find the code button because it preserves the indentation.

Please tell us what you think the flow through the program is; you should by now know that we expect people to have tried things themselves already.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
synchronized(b)

This line implies that the thread executing the synchronized block has a lock on the object b...??

try {
b.wait();
}

when wait() is invoked the thread gives up the lock on the object.

But I dont understand when synchronized(this) is called, it is referring to which object and which thread is executing it???
 
Campbell Ritchie
Marshal
Posts: 80093
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might get more answers if I move this thread to the threads forum.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Abhi vijay:
synchronized(b)

This line implies that the thread executing the synchronized block has a lock on the object b...??

try {
b.wait();
}

when wait() is invoked the thread gives up the lock on the object.

But I dont understand when synchronized(this) is called, it is referring to which object and which thread is executing it???



The 'this' reference is an object's self reference. So when the class does synchronized(this) it is getting the lock on itself. Which thread is executing it? Any thread that calls the run() method. From your understanding of Threads, when does the run() method get called for a Thread? The API for Thread may help.
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... and don't get into the habit of putting a wait outside a loop ...

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html#wait()

... in theory that code could give you odd behaviour .
 
This tiny ad will self destruct in five seconds.
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic