Thread Communication (Wait () notify ())

Source: Internet
Author: User

Import Java.util.concurrent.atomic.atomicinteger;public class Traditionalthreadcommunication {/** * @param args */ public static void Main (string[] args) {final business business = new Business (); New Thread (New Runnable () {@Override public void Run () {for (int i=1;i<=50;i++) {business.sub (i);}}} ). Start (); for (int i=1;i<=50;i++) {business.main (i);}}} Class Business {Private Boolean bshouldsub = True, public synchronized void sub (int. i) {while (!bshouldsub) {try {This.wa It (); } catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();}} for (int j=1;j<=10;j++) {System.out.println ("Sub thread sequence of" + j + ", loop of" + i);} Bshouldsub = false; This.notify (); synchronized void main (int i) {while (bshouldsub) {try {this.wait ()} catch (Interruptedexception e) {//TODO A Uto-generated Catch block E.printstacktrace (); }} for (int j=1;j<=100;j++) {System.out.println ("main thread sequence of" + j + ", loop of" + i);} BsHouldsub = true; This.notify (); } }

Thread Communication (Wait () 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.