multithreading - Java: How can the wait() and notify() methods be called on Objects that are not threads? -


how can wait() , notify() methods called on objects not threads? doesn't make sense, it?

surely, must make sense, however, because 2 methods available java objects. can provide explanation? having trouble understanding how communicate between threads using wait() , notify().

locks different threads. lock on data structure being protected. threads things accessing data structure. locks on data structure object in order keep threads accessing data structure in unsafe way.

any object can used intrinsic lock (meaning used in conjunction synchronized). way can guard access object adding synchronized modifier methods access shared data. (not it's idea, because allows thread can access object acquire lock, if it's not calling methods on it; it's better keep lock private member of data structure being locked, access limited.)

wait , notify called on objects being used locks. lock shared communication point:

  • when thread has lock calls notifyall on it, other threads waiting on same lock notified. when thread has lock calls notify on it, 1 of threads waiting on same lock gets notified.

  • when thread has lock calls wait on it, thread releases lock , goes dormant until either a) receives notification, or b) wakes arbitrarily (the "spurious wakeup"); waiting thread remains stuck in call wait until wakes due 1 of these 2 reasons, thread has re-acquire lock before can exit wait method.

see oracle tutorial on guarded blocks, drop class shared data structure, threads using producer , consumer runnables accessing it. locking on drop object controls how threads access drop object's data.

threads used locks in jvm implementation, application developers advised avoid using threads locks. instance, documentation thread.join says:

this implementation uses loop of this.wait calls conditioned on this.isalive. thread terminates this.notifyall method invoked. recommended applications not use wait, notify, or notifyall on thread instances.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -