Java multi-line thread Wait (), notify (), Notifyall () detailed analysis _java

Source: Internet
Author: User
Tags thread class
Wait (), notify (), Notifyall ( )does not belong to Thread Class, but belong to object base class, which means that each object has the function of Wait (), notify (), Notifyall ( )Because each object has a lock, the lock is the basis of each object, of course, the way to operate the lock is the most basic.

Waiting causes the current thread to wait until the other thread calls the object's Notify ()Method or Notifyall ()method, or interrupted by another thread. Wait can only be invoked by threads that hold a pair of like locks.

Notify wakes up a single thread waiting on this object monitor. If all threads wait on this object, they will choose to wake one of the threads (random). The awakened thread continues to execute until the current thread discards the lock on this object. Like the Wait method, notify can only be invoked by a thread that holds a pair of locks. Notifyall Also, the difference is that Notifyall will call all threads waiting on this object.
"Can only be invoked by a thread that holds an image lock." The Wait method and the Notify method must be executed within the synchronized block, that is, within synchronized (obj). Furthermore, it is not possible to synchronized the code block without a lock, so the thread must get the lock to continue executing. Complementary.
look at a very classic example (producer and consumer):
The first is the consumer thread class:
Copy Code code as follows:

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 ()//container empty, discard lock, wait for production
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
try {
Thread.Sleep (1000);
catch (Interruptedexception e) {
E.printstacktrace ();
}
Container.remove (0);
Container.notify ();
SYSTEM.OUT.PRINTLN ("I ate" + (++count) + "a");
}
}
}
}

Next is the producer thread class:
Copy Code code as follows:

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) {
If the container exceeds the maximum value, do not produce, wait for consumption
try {
Container.wait ();
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
try {
Thread.Sleep (1000);
catch (Interruptedexception e) {
E.printstacktrace ();
}
Container.add (New Object ());
Container.notify ();
System.out.println ("I produced" + (++count) + "a");
}
}
}
}

Finally, the test class:
Copy Code code as follows:

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 ();
}
Public List GetContainer () {
return container;
}
public void Setcontainer (List container) {
This.container = container;
}
}

The results of the run are as follows:
Copy Code code as follows:

I produced a
I ate one.
I produced 2.
I produced 3.
I produced 4.
I produced 5.
I produced 6.
I produced 7.
I ate 2.
I produced 8.
I ate 3.
I produced 9.
I ate 4.
I ate 5.
I ate 6.
I ate 7.
I ate 8.
I produced 10.
I produced 11.
I ate 9.
I produced 12.
I ate 10.
......

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.