Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Vs. Thread

 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can some one explain why the first thread runs printing A and B , where as the second thread throws an exception.
The source is http://www.javaranch.com


Is it because in the second program the object doesnot have a refernce name?




[ September 10, 2008: Message edited by: Nabila Mohammad ]
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second one throws MonitorStateException. This is because when you do this

synchronized(new Object())//separate object
{
new Object.notifyAll();//separate object
}

as you may know that you must hold the lock of the object that you call wait, notify and notifyAll on. But in this case, the synchronized block uses a different object and the notifyAll call uses a separate object of the Object class..
 
ankit kumar
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya in the second progrmme you are creating again the new Object . you are not using the previous object.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
Why didn't i think of that....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic