欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java多線程實(shí)現(xiàn)同時(shí)輸出

 更新時(shí)間:2016年03月13日 09:03:23   投稿:hebedich  
這篇文章主要介紹了Java多線程實(shí)現(xiàn)同時(shí)打印的相關(guān)資料,需要的朋友可以參考下

一道經(jīng)典的面試題目:兩個(gè)線程,分別打印AB,其中線程A打印A,線程B打印B,各打印10次,使之出現(xiàn)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();
   }
 }

由上面的例子我們可以設(shè)計(jì)出3個(gè)線程乃至于n個(gè)線程的程序,下面給出的例子是3個(gè)線程,分別打印A,B,C 10次,使之出現(xiàn)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();
  }
}

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

相關(guān)文章

  • 一篇文章帶你入門Java?UML的類圖

    一篇文章帶你入門Java?UML的類圖

    這篇文章主要為大家詳細(xì)介紹了Java?UML的類圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • myBatis組件教程之緩存的實(shí)現(xiàn)與使用

    myBatis組件教程之緩存的實(shí)現(xiàn)與使用

    這篇文章主要給大家介紹了關(guān)于myBatis組件教程之緩存的實(shí)現(xiàn)與使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • Java?詳解Map集合之HashMap和TreeMap

    Java?詳解Map集合之HashMap和TreeMap

    本章具體介紹了HashMap、TreeMap兩種集合的基本使用方法和區(qū)別,圖解穿插代碼實(shí)現(xiàn)。?JAVA成仙路從基礎(chǔ)開始講,后續(xù)會講到JAVA高級,中間會穿插面試題和項(xiàng)目實(shí)戰(zhàn),希望能給大家?guī)韼椭?/div> 2022-03-03
  • Java?Spring框架的注解式開發(fā)你了解嗎

    Java?Spring框架的注解式開發(fā)你了解嗎

    這篇文章主要為大家詳細(xì)介紹了Spring框架的注解式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • SpringCloudConfig之client端報(bào)錯(cuò)Could?not?resolve?placeholder問題

    SpringCloudConfig之client端報(bào)錯(cuò)Could?not?resolve?placeholder問

    這篇文章主要介紹了SpringCloudConfig之client端報(bào)錯(cuò)Could?not?resolve?placeholder?‘from‘?in?value?“${from}“問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助
    2022-12-12
  • SpringBoot如何通過Feign調(diào)用傳遞Header中參數(shù)

    SpringBoot如何通過Feign調(diào)用傳遞Header中參數(shù)

    這篇文章主要介紹了SpringBoot通過Feign調(diào)用傳遞Header中參數(shù),本文給大家分享兩種解決方案給大家詳細(xì)講解,需要的朋友可以參考下
    2023-04-04
  • idea2020導(dǎo)入spring5.1的源碼詳細(xì)教程

    idea2020導(dǎo)入spring5.1的源碼詳細(xì)教程

    這篇文章主要介紹了idea2020導(dǎo)入spring5.1的源碼的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Java中StringBuilder類的用法解析

    Java中StringBuilder類的用法解析

    StringBuilder是一個(gè)可變的字符序列,這個(gè)類提供了一個(gè)與StringBuffer兼容的API。本文主要為大家介紹了StringBuilder類的常用用法,需要的可以參考一下
    2023-05-05
  • IDEA中啟動多個(gè)SpringBoot服務(wù)的實(shí)現(xiàn)示例

    IDEA中啟動多個(gè)SpringBoot服務(wù)的實(shí)現(xiàn)示例

    本文主要介紹了IDEA中啟動多個(gè)SpringBoot服務(wù)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-08-08
  • Spring?Boot集成JavaMailSender發(fā)送郵件功能的實(shí)現(xiàn)

    Spring?Boot集成JavaMailSender發(fā)送郵件功能的實(shí)現(xiàn)

    spring提供了發(fā)送郵件的接口JavaMailSender,通過JavaMailSender可以實(shí)現(xiàn)后端發(fā)送郵件,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot集成JavaMailSender發(fā)送郵件功能的相關(guān)資料,需要的朋友可以參考下
    2022-05-05

最新評論