国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java多線程實現同時輸出

2019-11-26 14:30:20
字體:
來源:轉載
供稿:網友

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

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 德惠市| 澎湖县| 隆林| 清水河县| 天门市| 新营市| 灵宝市| 岢岚县| 漳浦县| 罗定市| 礼泉县| 柯坪县| 宜兰县| 玉屏| 蒙自县| 紫金县| 南康市| 夏邑县| 徐水县| 保靖县| 宝坻区| 平度市| 游戏| 阳谷县| 山西省| 龙陵县| 临城县| 临漳县| 华坪县| 祁东县| 多伦县| 定安县| 伊吾县| 石首市| 左云县| 揭东县| 全州县| 恩施市| 大邑县| 新源县| 太仆寺旗|