程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java多線程完成同時輸入

Java多線程完成同時輸入

編輯:關於JAVA

Java多線程完成同時輸入。本站提示廣大學習愛好者:(Java多線程完成同時輸入)文章只能為提供參考,不一定能成為您想要的結果。以下是Java多線程完成同時輸入正文


一道經典的面試標題:兩個線程,分離打印AB,個中線程A打印A,線程B打印B,各打印10次,使之湧現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();
   }
 }

由下面的例子我們可以設計出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();
  }
}

再一次證實了軟件工程的主要性了;在多線程法式中,應當說在法式中,我們應當把那些營業邏輯代碼放到統一個類中,使之高內聚,低耦合

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved