In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the difference between Condition.await, signal and Object.wait, notify". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what's the difference between Condition.await, signal and Object.wait, notify?"
The wait,notify and notifyAll methods in the Object class can be used to schedule between threads. For example, in the implementation of a blocking queue (BlockingQueue), if the queue is empty, all consumer threads wait. If a new element is added to the queue at some point, you need to wake up one or all of the blocked consumer threads (notify,notifyAll). Similarly, if the queue is full, all producer threads need to block. When an element is consumed, one or all of the blocking producer threads need to be awakened.
Condition's await,signal, singalAll and Object's wait,notify and notifyAll are all achievable requirements, both of which are very similar in use. They both need to acquire a lock before it can be called. The difference is that Object wait,notify corresponds to a lock in synchronized mode, while Condition await,singal corresponds to a lock corresponding to ReentrantLock (a lock object that implements Lock interface).
Let's take a look at the following specific example: using wait, notify and await, signal to implement a simple queue
Public interface SimpleQueueDemo {void put (E e); E take ();}
Implementation based on Object wait and notify
Public class SynchronizedQueue implements SimpleQueueDemo {private Object [] array; private int index = 0; public SynchronizedQueue (int size) {array = new Object [size] } @ Override public synchronized void put (E item) {while (isFull ()) {try {this.wait ();} catch (InterruptedException e) {e.printStackTrace () }} array [index++] = item; this.notifyAll () } @ Override @ SuppressWarnings ("unchecked") public synchronized E take () {while (isEmpty ()) {try {this.wait ();} catch (InterruptedException e) {e.printStackTrace () }} E item = (E) array [0]; array = Arrays.copyOfRange (array, 1, array.length + 1); array [array.length-1] = null; index--; this.notifyAll () Return item;} private boolean isFull () {return index > = array.length;} private boolean isEmpty () {return index = array.length;} private boolean isEmpty () {return index
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.