Wait () and notify () implement the communication between threads

Source: Internet
Author: User

It programmer development Essentials-all kinds of resources download list, history of the most IT resources, personal collection summary.
Example program:

Package Edu.review;   public class Traditionalthreadcommunication {/** * Title requirements: * Sub-thread loop 10 times, then the main thread loop 20 times, and then back to the child thread loop 10 times, and then back to the main thread loop 20 times, *
	 This cycle is 50 times, please write the program.
         */public static void main (string[] args) {final business business = new Business (); New Thread (New Runnable () {@Override public void run () {for (int i = 0; i <, i++) {business.
				Sub (i);
         }}). Start ();
		 for (int i = 0; i < i++) {business.main (i);
	}}} Class business{private Boolean bshouldsub = true; Public synchronized void sub (int loop) {/**          * Here while you can actually use if instead, except W
The security of the hile is higher than if, in order to prevent "false wakeup" from happening.          * Because the JVM allocates the CPU to which thread is random, it can only be processed in the inline code block        &nbsp        ; * When the child thread does not execute, then this.wait (), the child thread locks the object and then the JVM assigns the object lock to other threads          * Note:    &nbs p;     *  &nbsp The object that calls the Wait () and notify () methods must be the same object as the lock monitoring object          **/while (!bshouldsub) {try {T
			His.wait ();
			} catch (Interruptedexception e) {e.printstacktrace ();
		}} for (int i = 0; i < i++) {System.out.println ("Sub thread sequence of" +i+ ", Loop of" +loop);
		}//The child thread executes the code, sets Bshouldsub to false, and then wakes up a single thread waiting on this object monitor bshouldsub = false;
	This.notify ();
			synchronized void main (int loop) {while (bshouldsub) {try {this.wait ()};
			} catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
		}} for (int i = 0; i <; i++) {System.out.println ("main thread sequence of" +i+ ", Loop of" +loop ");
		} bshouldsub = true;
	This.notify (); }
	
}


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.