• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Doubt  RSS feed

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • 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 ]
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might get more answers if I move this thread to the threads forum.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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 .
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!