Two threads of Java multi-thread communication print AB each 10 times, and java multi-thread Communication
A typical interview question: two threads print AB respectively, where thread A prints A and thread B prints B, each of which prints 10 times, so that the result of ABABABABA...
1 package com. shangshe. path; 2 3 public class ThreadAB {4 5/** 6 * @ param args 7 */8 public static void main (String [] args) {9 10 final Print business = new Print (); 11 12 new Thread (new Runnable () {13 public void run () {14 for (int I = 0; I <10; I ++) {15 business. print_A (); 16} 17} 18 }). start (); 19 20 new Thread (new Runnable () {21 public void run () {22 for (int I = 0; I <10; I ++) {23 business. print_ B (); 24} 25} 26}). start (); 27 28} 29} 30 class Print {31 32 private boolean flag = true; 33 34 public synchronized void print_A () {35 while (! Flag) {36 try {37 this. wait (); 38} catch (InterruptedException e) {39 // TODO Auto-generated catch block40 e. printStackTrace (); 41} 42} 43 System. out. print ("A"); 44 flag = false; 45 this. notify (); 46} 47 48 public synchronized void print_ B () {49 while (flag) {50 try {51 this. wait (); 52} catch (InterruptedException e) {53 // TODO Auto-generated catch block54 e. printStackTrace (); 55} 56} 57 System. out. print ("B"); 58 flag = true; 59 this. notify (); 60} 61}View Code
From the above example, we can design three threads and n threads. The following example shows three threads that print A, B, and C 10 times respectively, make it appear ABCABC .. effect
1 public class ThreadABC {2 3/** 4 * @ param args 5 */6 public static void main (String [] args) {7 8 final Print business = new Print (); 9 10 new Thread (new Runnable () {11 public void run () {12 for (int I = 0; I <100; I ++) {13 business. print_A (); 14} 15} 16 }). start (); 17 18 new Thread (new Runnable () {19 public void run () {20 for (int I = 0; I <100; I ++) {21 business. print_ B (); 22} 23} 24 }). start (); 25 26 new Thread (new Runnable () {27 public void run () {28 for (int I = 0; I <100; I ++) {29 business. print_C (); 30} 31} 32 }). start (); 33 34} 35} 36 class Print {37 38 private boolean should_a = true; 39 private boolean should_ B = false; 40 private boolean should_c = false; 41 42 public synchronized void print_A () {43 while (should_ B | should_c) {44 try {45 this. wait (); 46} catch (InterruptedException e) {47 // TODO Auto-generated catch block48 e. printStackTrace (); 49} 50} 51 System. out. print ("A"); 52 should_a = false; 53 should_ B = true; 54 should_c = false; 55 this. policyall (); 56} 57 58 public synchronized void print_ B () {59 while (should_a | should_c) {60 try {61 this. wait (); 62} catch (InterruptedException e) {63 // TODO Auto-generated catch block64 e. printStackTrace (); 65} 66} 67 System. out. print ("B"); 68 should_a = false; 69 should_ B = false; 70 should_c = true; 71 this. policyall (); 72} 73 74 public synchronized void print_C () {75 while (should_a | should_ B) {76 try {77 this. wait (); 78} catch (InterruptedException e) {79 // TODO Auto-generated catch block80 e. printStackTrace (); 81} 82} 83 System. out. print ("C"); 84 should_a = true; 85 should_ B = false; 86 should_c = false; 87 this. policyall (); 88} 89}View Code
I once again proved the importance of software engineering. In multi-threaded programs, we should say that in the program, we should put the business logic code in the same class to make it highly cohesive and low coupling.