ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

In Java why wait() and notify() methods are in Object class but used in thread?

In java why wait() and notify() method are present in object class but used in Thread?

user-image
تم إضافة السؤال من قبل Zishan Khan , Sr software engineer , niit technology
تاريخ النشر: 2017/12/22
مستخدم محذوف‎
من قبل مستخدم محذوف‎

wait()=

If threads calls wait() on any object it immeditaly release the lock of the particular object and entered in to waiting state .

notify()=

If threads calls notify() on any object it  release the lock of the particular object butmay not immeditaly .

 

مستخدم محذوف‎
من قبل مستخدم محذوف‎

wait

Object wait methods has three variance, one which waits indefinitely for any other thread to call notify or notifyAll method on the object to wake up the current thread. Other two variances puts the current thread in wait for specific amount of time before they wake up.

 

notify

notify method wakes up only one thread waiting on the object and that thread starts execution. So if there are multiple threads waiting for an object, this method will wake up only one of them. The choice of the thread to wake depends on the OS implementation of thread management.

Anupama Patil
من قبل Anupama Patil , GIS Associate , Wipro LTD

These methods are communication mechanism between two threads in java. object class is the correct place to make them available for every object. and locks are made available ob per object basis. and these () works on locks,and locks are in object.

Devendra Koppu
من قبل Devendra Koppu

Every object in java has only one lock(monitor) and wait(),notify(),notifyAll() are used for monitor sharing thats why they are part of object class rather than Thread class. These methods works on the locks and locks are associated with Object and not Threads

Ahmed Salem
من قبل Ahmed Salem , Senior Software Developer , Silicon Egypt Technologies

Because Locks are made available on per Object basis, which is another reason wait and notify is declared in Object class rather then Thread class.

 

مستخدم محذوف‎
من قبل مستخدم محذوف‎

wait means it's having some time to wait.exceptions used in java.that is very big concept in java.checked exception and unchecked execption.wait is checketed exception and thread is unchecked execpetion.

المزيد من الأسئلة المماثلة