• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

wait with/without synchronized block

 
Ranch Hand
Posts: 57
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, this code is taken from K&B book :

when we remove synchronized we got an runtime exception because there is no synchronized object with laurel !
but when we add the synchronized block on lauref normally we got A C and then D we wait forever because there is notify from any laurel object ? am i right ?
but when we run this code we got :

A D C F

Thanks for your help!
Best regards
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adam Satyres wrote:
but when we add the synchronized block on lauref normally we got A C and then D we wait forever because there is notify from any laurel object ? am i right ?
but when we run this code we got :

A D C F



You have a conflict with the core library. Don't use Thread objects for wait/notify -- it is used internally by the core library to implement the join() method.

Henry
 
Ranch Hand
Posts: 49
1
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry, what do you mean by core library?

I'm not sure why F is being printed either. I ran the code and every time F is printed.
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Mercier wrote:Henry, what do you mean by core library?

I'm not sure why F is being printed either. I ran the code and every time F is printed.



The core library are the java classes that are included with the JVM. And the conflict that was mentioned earlier, is the reason why "F" is printed.

Henry
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sometimes the jvm call the notify() methode ,but for the exam the wait should be notified
 
reply
    Bookmark Topic Watch Topic
  • New Topic