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

Why is it required to have a synchronized code to call notify() & other related methods from Object?

 
Ranch Hand
Posts: 40
2
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Could you please elaborate, why is it required to have a synchronized code to call notify() & other related methods from Object?

Regards,
Kaleem
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go to pdf here and read topic 14. It explains it in very good way.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes, when dealing with concurrent code, one task has to wait for another task to finish before it can finish itself. Tasks can signal one another through the wait()/notify()/notifyAll() methods.

The wait() method can be called in a loop checking a condition to make a task wait until the condition holds.

The notify() and notifyAll() methods can be used by a task that influences whether the condition holds, to make waiting tasks recheck the condition.

We now have the Concurrency API, with types such as Lock, CountDownLatch and Semaphore. If the language designers had added these in the first version of Java, then they could have left the wait(), notify() and notifyAll() methods out.
 
Kaleem Anwar
Ranch Hand
Posts: 40
2
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

We now have the Concurrency API, with types such as Lock, CountDownLatch and Semaphore. If the language designers had added these in the first version of Java, then they could have left the wait(), notify() and notifyAll() methods out.



Is this mean that these methods are deprecated or will be deprecated in future?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, because the Concurrency API is built on top of them. I suppose it's also valid to use them on embedded systems because it may be more efficient to use these primitives directly.

However, when you're writing new code, you probably never have a reason to use them; using the classes in the Concurrency API should be enough.
 
author
Posts: 23951
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

As a side note, this is true for all threading implementations of condition variables. Whether you use the Windows API, or the POSIX API, or Solaris, etc. practically all implementations of condition variables, require that the mutex is held, and to be managed by the condition variable.

The reason for this is because there are race conditions that can't be dealt with, unless the mutex and condition variable are tightly integrated.

Henry
 
Not so fast naughty spawn! I want you to know about
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic