In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 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 how to write the key code of java doc threads. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
Java doc threads require us to keep learning about them when using them. Let's take a detailed look at how to make better use of the relevant code. Wait (), notify (), notifyAll () do not belong to the Thread class, but belong to the Object basic class, that is, each object has the function of wait (), notify (), notifyAll ().
Because every object has a lock, the lock is the basis of every object, and of course the method of operating the lock is also the most basic.
First, let's see what the java doc thread says:
Wait causes the current thread to wait until another thread calls the object's notify () method or notifyAll () method. The current thread must have this object monitor. The thread publishes ownership of the monitor and waits until another thread wakes up by calling the notify method, or the notifyAll method notifies the waiting thread on this object's monitor. The thread will then wait to regain ownership of the monitor before continuing execution.
Java thread synchronization lock solves the security of shared data
How to avoid this tragedy by Java thread deadlock
How to improve the related data processing of Java threading model
How can Java thread synchronization eliminate blocking
The key code of Java thread function is described in detail.
Notify wakes up a single thread waiting on this object monitor. 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) pass the 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 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 } above is how the editor shares the key code of java doc threads. 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.