Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the concrete classification of Java multithread statements

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to analyze the specific classification of Java multithreaded statements? in view of this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

There are many small statements in Java multithreaded statements that require our special attention. Wait (), notify (), notifyAll () do not belong to the Thread class, so let's take a look at how to use these classification codes in detail. I hope you all get something.

It belongs to the Object basic class, that is, every object has the functions 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 java doc says:

In a Java multithreaded statement, 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.

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 target 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:

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; imort 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 } this is the answer to the question on how to analyze the specific classification of Java multithreaded statements. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report