Java 멀티스레드 동시 출력

4190 단어 Java 멀티스레드
고전적인 면접 문제: 두 개의 라인, 각각 AB, 그 중에서 라인 A 프린트 A, 라인 B 프린트 B, 각각 10회 프린트하여 ABABA가 나타나도록 한다.효과

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

위의 예에서 우리는 3개의 라인, 더 나아가 n개의 라인을 설계할 수 있다. 아래에서 제시한 예는 3개의 라인으로 각각 A, B, C를 10회 인쇄하여 ABCABC가 나타나도록 한다.효과

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

소프트웨어 공학의 중요성을 다시 한 번 증명했다.다중 루틴 프로그램에서, 프로그램에서, 우리는 그 업무 논리 코드를 같은 클래스에 넣어서, 높은 내적 집합, 낮은 결합을 시켜야 한다고 말해야 한다

좋은 웹페이지 즐겨찾기