Home  >  Article  >  Java  >  Java multi-threading to achieve simultaneous output

Java multi-threading to achieve simultaneous output

高洛峰
高洛峰Original
2017-01-05 16:58:341719browse

A classic interview question: Two threads print AB respectively. Thread A prints A and thread B prints B. Each prints 10 times to make it appear ABABABABA..

package com.shangshe.path;
 
public class ThreadAB {
 
  /**
  * @param args
  */
  public static void main(String[] args) {
     
    final Print business = new Print();
     
    new Thread(new Runnable() {
      public void run() {
        for(int i=0;i<10;i++) {
          business.print_A();
        }
      }
    }).start();
     
    new Thread(new Runnable() {
      public void run() {
        for(int i=0;i<10;i++) {
          business.print_B();
        }
      }
    }).start();
     
  }
}
class Print {
   
  private boolean flag = true;
   
  public synchronized void print_A () {
    while(!flag) {
      try {
        this.wait();
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    System.out.print("A");
    flag = false;
    this.notify();
  }
   
  public synchronized void print_B () {
    while(flag) {
      try {
        this.wait();
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    System.out.print("B");
    flag = true;
    this.notify();
  }
}

From the above For example, we can design a program with 3 threads or even n threads. The example given below is 3 threads, printing A, B, C 10 times respectively, so that the effect of ABCABC.. appears

public class ThreadABC {
 
  /**
   * @param args
   */
  public static void main(String[] args) {
     
    final Print business = new Print();
     
    new Thread(new Runnable() {
      public void run() {
        for(int i=0;i<100;i++) {
          business.print_A();
        }
      }
    }).start();
     
    new Thread(new Runnable() {
      public void run() {
        for(int i=0;i<100;i++) {
          business.print_B();
        }
      }
    }).start();
     
    new Thread(new Runnable() {
      public void run() {
        for(int i=0;i<100;i++) {
          business.print_C();
        }
      }
    }).start();
     
  }
}
class Print {
   
  private boolean should_a = true;
  private boolean should_b = false;
  private boolean should_c = false;
   
  public synchronized void print_A () {
    while(should_b || should_c) {
      try {
        this.wait();
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    System.out.print("A");
    should_a = false;
    should_b = true;
    should_c = false;
    this.notifyAll();
  }
   
  public synchronized void print_B () {
    while(should_a || should_c) {
      try {
        this.wait();
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    System.out.print("B");
    should_a = false;
    should_b = false;
    should_c = true;
    this.notifyAll();
  }
   
  public synchronized void print_C () {
    while(should_a || should_b) {
      try {
        this.wait();
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    System.out.print("C");
    should_a = true;
    should_b = false;
    should_c = false;
    this.notifyAll();
  }
}

Once again proves the importance of software engineering; in multi-threaded programs, it should be said that in programs, we should put those business logic codes into the same class to make them high cohesion and low coupling


For more articles related to Java multi-threading and simultaneous output, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn