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

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

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 动漫a级片| 妹妹你插的我好爽 | 日韩毛片基地一区二区三区 | 小寡妇好紧进去了好大看视频 | 久草在线精彩免费视频 | 变形金刚第一部 | 范冰冰a级一级特级毛片 | 日韩在线视精品在亚洲 | 无套日出白浆在线播放 | 热久久最新地址 | 国产午夜亚洲精品理论片不卡 | 11 13加污女qq看他下面 | 色综七七久久成人影 | 白丝美女用胸伺候主人 | 成人影院在线观看免费 | 美女脱了内裤打开腿让男人图片 | 胸奶好大好紧好湿好爽 | 母性本能 | 亚洲欧美成人中文在线网站 | 亚洲国产一区二区三区a毛片 | 美女林柏欣21p人体之仓之梦 | 女人肮脏的交易中文字幕未删减版 | 性一交一乱一伧老太 | 亚洲无限观看 | 精品国产美女福利在线 | 女人张开腿让男人桶爽 | 欧美男同猛男 videos 同性 | 成人网视频免费播放 | 日韩精品一区二区三区视频 | 亚洲人成网站在线观看妞妞网 | 日本国产一区二区三区 | 果冻传媒在线视频观看免费 | 久久无码人妻中文国产 | 色噜噜狠狠色综合 | 国产拍拍拍免费专区在线观看 | 午夜国产精品视频在线 | 大香焦在线| 精品午夜久久福利大片免费 | 国产精品成人免费福利 | 香蕉91视频 | 青草娱乐极品免费视频 |