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 ())