服务器之家:专注于服务器技术及软件下载分享
分类导航

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

再一次证明了软件工程的重要性了;在多线程程序中,应该说在程序中,我们应该把那些业务逻辑代码放到同一个类中,使之高内聚,低耦合

延伸 · 阅读

精彩推荐