Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

Why wait and notify is declared in Object class instead of Thread?

Sumit Rawal answered on August 28, 2023 Popularity 1/10 Helpfulness 1/10

Contents


More Related Answers

  • Explain wait(), notify() and notifyAll() methods of object class ?
  • Explain why wait(), notify() and notifyAll() methods are in Object class rather than in thread class?

  • Why wait and notify is declared in Object class instead of Thread?

    0

    The wait() and notify() methods are declared in the Object class in Java rather than in the Thread class for reasons related to synchronization and the monitor pattern.

    Monitor Pattern:

    The wait() and notify() methods are fundamental to implementing the monitor pattern, which allows threads to coordinate their actions and safely communicate with each other using shared objects. The monitor pattern provides a way to ensure that only one thread can execute a synchronized block of code at a time.

    Synchronization of Shared Objects:

    The wait() and notify() methods are used to synchronize access to shared objects. Placing these methods in the Object class allows any Java object to be used as a monitor for synchronization. This is important because synchronization often involves coordinating multiple threads around shared resources, and those resources may not necessarily be instances of the Thread class.

    Avoiding Direct Interaction with Threads:

    Placing synchronization methods in the Object class helps promote a higher level of abstraction and separation of concerns. Developers don't need to interact directly with thread objects to manage synchronization; instead, they work with shared objects and their intrinsic locks (monitors).

    Avoiding Mixing Concerns:

    If synchronization methods were placed in the Thread class, it would mix concerns related to synchronization with the responsibilities of thread management. By keeping synchronization methods in the Object class, Java maintains a clearer separation of responsibilities. 

    Popularity 1/10 Helpfulness 1/10 Language java
    Source: Grepper
    Link to this answer
    Share Copy Link
    Contributed on Aug 28 2023
    Sumit Rawal
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.