Java多線程實現同時輸出_java

來源:互聯網
上載者:User

一道經典的面試題目:兩個線程,分別列印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();  }}

再一次證明了軟體工程的重要性了;在多線程程式中,應該說在程式中,我們應該把那些商務邏輯代碼放到同一個類中,使之高內聚,低耦合

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.