In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you an example analysis of the Java notify wake-up source code. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Java notify wakes up a single thread waiting on this object monitor. The related problems require us to keep learning, so let's see how to make better use of them. If all threads are waiting on this object, one of the threads is chosen to wake up.
The awakened thread cannot continue execution until the current thread discards the lock on this object. This method should only be called by the thread that is the owner of this object monitor.
"the current thread must own this object monitor" and "this method should only be called by the thread that is the owner of this object monitor" indicates that the wait method and the notify method must be executed within the synchronization block, that is, synchronized (within the obj).
After calling the object wait method, the current thread releases the object lock and enters the waiting state. Until other threads (and only other threads) wake up through the Java notify method, or notifyAll. The thread reacquires the object lock.
Continue execution, remembering that the thread must reacquire the object lock to continue execution. Because you can't take an inch without a lock in a synchronized code block. Look at a classic example:
Java notify wake-up code
Package ProductAndConsume; import java.util.List; public class Consume implements Runnable {private List container = null; private int count; public Consume (List lst) {this.container = lst;} public void run () {while (true) {synchronized (container) {if (container.size () = = 0) {try {container.wait (); / / abandon lock} catch (InterruptedException e) {e.printStackTrace ();}} try {Thread.sleep } catch (InterruptedException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} container.remove (0); container.notify (); System.out.println ("I ate" + (+ + count) + "one");} package ProductAndConsume; import java.util.List; public class Product implements Runnable {private List container = null; private int count; public Product (List lst) {this.container = lst } public void run () {while (true) {synchronized (container) {if (container.size () > MultiThread.MAX) {try {container.wait ();} catch (InterruptedException e) {e.printStackTrace ();}} try {Thread.sleep (100);} catch (InterruptedException e) {e.printStackTrace ();} container.add (new Object ()); container.notify () System.out.println ("I have produced" + (+ + count) + "a");} package ProductAndConsume; import java.util.ArrayList; import java.util.List; public class MultiThread {private List container = new ArrayList (); public final static int MAX = 5; public static void main (String args []) {MultiThread m = new MultiThread (); new Thread (new Consume (m.getContainer (). Start () New Thread (new Product (m.getContainer ()). Start (); new Thread (new Consume (m.getContainer (). Start (); new Thread (new Product (m.getContainer ()). Start ();} public List getContainer () {return container;} public void setContainer (List container) {this.container = container } the above is the example analysis of the Java notify wake-up source code shared by Xiaobian. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.