検索
ホームページJava&#&チュートリアル同時出力を実現する Java マルチスレッド

同時出力を実現する Java マルチスレッド

Jan 05, 2017 pm 04:58 PM
Javaマルチスレッド

古典的なインタビューの質問: 2 つのスレッドがそれぞれ AB を出力します。スレッド A は A を出力し、スレッド B は B を出力します。ABABABA の効果を与えるためにそれぞれ 10 回出力します。

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();
  }
}

の効果は、マルチスレッドにおけるソフトウェア エンジニアリングの重要性を再度証明しています。プログラムでは、これらのビジネス ロジック コードを同じクラスに配置して、結合性を高め、低結合にする必要があると言うべきです


さらに Java マルチスレッドで同時出力を実現 関連記事はこちらPHP中国語ウェブサイトへ!


声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

VSCode Windows 64 ビットのダウンロード

VSCode Windows 64 ビットのダウンロード

Microsoft によって発売された無料で強力な IDE エディター

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン