[Turn]java why Wait (), notify (), Notifyall () must be called in the synchronous Method/code block?

Source: Internet
Author: User

In Java, all objects can be used as "monitor monitors"-an entity that has an exclusive lock, an ingress queue, and a waiting queue.

The non-synchronous methods of all objects can be called at any time by any thread, and there is no need to consider locking issues at this time.

For the synchronization method of an object, there are only a few threads that have an exclusive lock on that object to invoke them at any one time. For example, a synchronization method is exclusive. If a thread calls the synchronization method of an object, the exclusive lock of the object is owned by another, and then the front thread is blocked and added to the object's entry queue .

You can invoke the Wait (), notify (), and Notifyall () methods of an object only if the calling thread owns an exclusive lock on the objects . If you attempt to invoke these three methods without acquiring an object lock, you will get a "java.lang.IllegalMonitorStateException:current thread not owner".   when a thread is impersonating in a synchronous method of an object invokes the wait () method of the object, the thread frees the object's exclusive lock and is placed in the waiting queuefor that object ( JZ: means that other threads can also invoke the wait method of the same object againNote that theWait () method forces the current thread to release the object lock. This means that the current thread must have acquired a lock on an object before it is called by the Wait () method.  Therefore, a thread must be in a synchronous method or a synchronous code block of an object to invoke the Wait () method of that object . when a thread calls an object's notify () or Notifyall () method, either (for Notify ()) or all (for Notifyall ()) threads in that object's wait queue are transferred to the object's ingress queue . Then these queues (the translator note: Maybe only one) will compete for the lock on that object, and the thread that eventually gets the lock continues to execute. if no thread waits for a lock in the waiting queue of the object, then notify () and Notifyall () will have no effect. before calling the object's notify () and Notifyall () methods, the calling thread must have obtained the lock on the object . Therefore, the Notify () or Notifyall () method of the object must be called in a synchronization method or in a synchronized code block of an object. for a thread that is in the waiting queue for an object, the opportunity to continue execution is only available when other threads call the Notify () or Notifyall () method of this object. the reason for calling the wait () method is usually that the calling thread wants a special state (or variable) to be set before continuing. The reason for calling the Notify () or Notifyall () method is usually that the calling thread wants to tell other waiting threads: "Special state has been set". This state acts as a channel for communication between threads, and it must be a mutable shared state (or variable). For example, the producer thread writes data to the buffer, and the consumer thread reads the data from the buffer. Consumer threads need to wait until the producer thread completes a write operation. The producer thread waits for the consumer thread to complete a read operation. Suppose that wait (), notify (), Notifyall () method do not need to be locked to be able to be called. at this point the consumer thread calls wait () to enter the status variable waiting queue (translator Note: May not yet be entered). At the same time, the producer thread calls the Notify () method to notify the consumer thread of the state change. Then the consumer thread will miss this notification and block it all the time. Therefore, the object's Wait (), notify (), Notifyall () method must be called mutually exclusive in the object's synchronization method or in a synchronous code block. [JZ]: This is also a problem in multithreaded programming, thread A is the process of locking operation is non-atomic, thread B has made a request to release, causing thread A to request a lock can not be released! Original: http://www.xyzws.com/Javafaq/why-wait-notify-notifyall-must-be-called-inside-a-synchronized-method-block/127reprint: http://m.blog.csdn.net/blog/qilixiang012/44524561

[Turn]java why Wait (), notify (), Notifyall () must be called in the synchronous Method/code block?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.