The difference between java condition await() and object wait()

The difference between java condition await() and object wait()

object wait()
* The current thread must own this object's monitor. The thread
     * releases ownership of this monitor and waits until another thread
     * notifies threads waiting on this object's monitor to wake up
     * either through a call to the {@code notify} method or the
     * {@code notifyAll} method. The thread then waits until it can
     * re-obtain ownership of the monitor and resumes execution.

The key point of the object method is the current thread And the listener of the object, calling this method will release the listener of the current thread for this object, and the current thread is also in a waiting blocking state, provided that the current thread has acquired the listener of the CPU,

condition await() is aimed at the current thread Scheduling, has nothing to do with the object, calling this method will release the LOCK of the current thread, and the current thread will wait for the condition to wait for the queue, waiting for other conditions to call dosignal()
will be awakened, and the current thread will be put back into the synchronization queue to wait for acquisition Lock,

The two are somewhat similar in performance, but the mechanism is different.
1. object wait() cannot be used alone, it must be used under synchronized,
2. object wait() must be awakened by the Nodify() method
3. Condition await() must be used after the current thread is excluded from the lock, and can only be used after the condition is obtained.
4. Condition await() must be woken up by the sign() method.

One is based on the synchronization method of object listeners, and the other is based on ASQ synchronization. Synchronization of the mechanism

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326864286&siteId=291194637