The communication between Java threads and the difference between notify and Notifyall.

Source: Internet
Author: User

The communication between multiple threads of the JVM is made up of thread locks, conditional statements, and wait (), notify ()/notifyall.

The following is a two different statement that implements an output that enables multiple threads to loop.

Package com.app.thread;

Import Javax.swing.plaf.SliderUI;
/**
* To see the problem
* @author Gordon
*
*/
public class Lockdemo {
public static void Main (string[] args) {
SYSTEM.OUT.PRINTLN ("lock");

Final outturn ot = new Outturn ();

for (int j=0;j<100;j++) {

New Thread (New Runnable () {

public void Run () {
try {
Thread.Sleep (10);
} catch (Interruptedexception e) {
E.printstacktrace ();
//     }
for (int i = 0; I <5; i++) {
Ot.sub ();
}
}
}). Start ();

New Thread (New Runnable () {

public void Run () {
try {
Thread.Sleep (10);
} catch (Interruptedexception e) {
E.printstacktrace ();
//     }
for (int i = 0; i < 5; i++) {
Ot.main ();
}
}
}). Start ();
}

}
}

Class Outturn {
Private Boolean issub = true;
private int count=0;

Public synchronized void sub () {
try {
while (!issub) {
This.wait ();
}
System.out.println ("sub----" +count);
Issub=false;
This.notify ();
} catch (Exception e) {
E.printstacktrace ();
}
count++;

}

Public synchronized void Main () {
try {
while (issub) {
This.wait ();
}

Issub=true;
This.notify ();
} catch (Exception e) {
E.printstacktrace ();
}
count++;
}
}

I do not know whether to see the problem, the first time there was a problem, looked for a long time to find out, has been no attention to the use of Notify and Notifyall (), this caused a big mistake, hey ...

Explain the difference between notify and Notifyall:

Both the sub and main methods use locks, so multiple calls to the sub method and multiple calls to the main method are blocked, waiting for a running thread to wake them up, and the above code wakes up with notify, and notify only wakes up one thread. The other waiting threads are still in the wait state, and if the thread that calls the Sub method finishes executing, all the threads are in the waiting state, the Issub=false is awakened by a sub method dispatch thread, and the while loop equals True, the thread is also waiting. A deadlock occurs when all the threads are waiting and no thread is running to wake them. If you use Notifyall () to wake up all the threads that are waiting for the lock, then all the threads will be in pre-run readiness, after the sub method has finished executing, waking up all the states waiting for the lock, even if you wake up a sub method to dispatch the thread, then the thread is waiting again. There are other threads that can get the lock and go into the running state. So the Notify method can easily cause deadlocks, unless you're programmed to determine that it doesn't cause a deadlock, and Notifyall is the thread's safe wake-up method.

To start with, the above code simply needs to change the parameters in the sub and main methods to This.notifyall ().

The communication between Java threads and the difference between notify and Notifyall.

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.