Heim > Java > javaLernprogramm > Hauptteil

Java-Multithreading zur Erzielung einer gleichzeitigen Ausgabe

高洛峰
Freigeben: 2017-01-05 16:58:34
Original
1728 Leute haben es durchsucht

Eine klassische Interviewfrage: Zwei Threads drucken jeweils AB, wobei Thread A A und Thread B B druckt, jeder druckt 10 Mal, damit es ABABABABA erscheint. Effekt

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();
  }
}
Nach dem Login kopieren

Aus dem obigen Beispiel , wir können ein Programm mit 3 Threads oder sogar n Threads entwerfen, wobei A, B, C jeweils 10 Mal gedruckt werden, sodass der Effekt von ABCABC erscheint

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();
  }
}
Nach dem Login kopieren

Dies beweist einmal mehr die Bedeutung der Softwareentwicklung. Bei Programmen mit mehreren Threads sollte gesagt werden, dass wir diese Geschäftslogikcodes in dieselbe Klasse einordnen sollten, um eine hohe Kohäsion und eine geringe Kopplung zu erreichen


Weitere Artikel zur Java-Multithreading-Implementierung und zur gleichzeitigen Ausgabe finden Sie auf der chinesischen PHP-Website!


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!