一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java多線程實現同時輸出

Java多線程實現同時輸出

2020-04-10 15:17hebedich JAVA教程

這篇文章主要介紹了Java多線程實現同時打印的相關資料,需要的朋友可以參考下

一道經典的面試題目:兩個線程,分別打印AB,其中線程A打印A,線程B打印B,各打印10次,使之出現ABABABABA.. 的效果

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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.. 的效果

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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();
  }
}

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91九色露脸 | 欧美艳星julnaann | 2019亚洲男人天堂 | 无遮挡激情| 国产成人精品曰本亚洲77美色 | 国产成人精品免费 | 国产国语在线播放视频 | 国内体内she精视频免费 | 欧美又大又粗又长又硬 | 国产一二三区视频 | 免费二级毛片免费完整视频 | 国模孕妇季玥全部人体写真 | 咪咪爱网友自拍 | 青青在线视频免费 | 双子母性本能在线观看 | 国产成人精品高清在线观看99 | 美国复古性经典xxxxx | 欧洲肥女大肥臀 | 免费一级欧美片在线观看 | bban女同系列022在线观看 | 色妞视频一级毛片 | ai换脸明星专区在线观看 | 婷婷麻豆 | 农村妇女野外牲交一级毛片 | 亚洲99久久无色码中文字幕 | 国产精品热久久毛片 | 国产精品自在线拍 | 性吧有你| 日本xx高清视频免费观看 | 思敏1一5集国语版免费观看 | 暖暖 免费 高清 日本 在线 | 免费观看美女被cao视频 | 亚洲激情网站 | 暖暖中国免费观看高清完整版 | 射18p| 日本视频免费在线观看 | 精品夜夜澡人妻无码AV蜜桃 | 亚洲福利一区二区三区 | 九九在线免费视频 | 湿好紧太硬了我太爽了 | 鸭子玩富婆流白浆视频 |